Why does my form `->required()` methods not work inside of a Livewire component?
Hey all, so I am creating a 'Settings' page which has a collection of livewire components.
I am trying to use a Radio input and a File upload input like so:
If I replace these components with say a Text input, it works as expected but the code above, attempts to submit even without data. Now if I copy and paste these into one of my resource forms, the validation works correctly.
Not entirely sure what is going on and happy to provide more code etc.
18 Replies
What does you
submit()
method look like?Sorry, was away doing a hotfix.
Let me grab that for you.
at the minute it looks like this:
however that is because I stripped it back, it was like this:
I really wish it didn't copy whitespace from jetbrains products!
Did you add the mount method, statePath?
https://filamentphp.com/docs/3.x/forms/adding-a-form-to-a-livewire-component#adding-the-form
Yes indeed, here is my whole file to make life a little easier.
?
Inside of the save method?
yes
huh, doing that makes the validation work, and if I remove the
dd
it also works so I guess I just need to change my method to access the form state first.
This gives a slightly different way of handling it but I should be able to sort it from here I think. I will keep this open until I have however.getState()
will validate the form
You should follow this section https://discord.com/channels/883083792112300104/1205152125441671238/1205172334974214184Ah where I messed up was copying another, similar component where the validation was working without this step which lead me down the wrong path!
Thank you Leandro!
Marked your original reply as the solution and updated the tag.
I have the same issue, but for some weird reason! calling
$this->form->getState();
in the submit function returns an empty array, and $this->form;
returns an empty form instance, however $this-data
returns the data correctly;please share the whole code
I moved the logic to
boot()
instead of mount()
and everything worked perfectly!Why would you call
$this->form->getState()
during mount?Sorry, I think there is a misunderstanding here. I said, calling
this->form->getState()
in submit function.Okay, but what did you move to
boot()
? 🤔Here is the code https://pastebin.com/dmmvFnpt
@Dennis Koch never mind, I found a lot of mistakes in my code. Thank you