statePath variable missing some form fields
so i have a few forms on the same page, using multiple forms like this. a couple of those forms have a problem, where only 2-3 of their fields, seemingly at random, will be "detached" from the form, i.e. the console throws a livewire error about "data.property" not found on component, and when inputting in those fields the data object will not update, and when trying to submit the form it will give a "property is required" error as if the field is empty (which it technically is since it never got updated inside the data variable). i've managed to fix this by changing my data definition from
public ?array $data = [];
to public ?array $data = ["property" => ""];
, basically manually initializing only the problematic fields. this way the form works perfectly, including the non-problematic fields that i didn't manually initialize aswell as the problematic fields that are now fixed since i've manually initialized them. the fields themselves don't really have anything special and are delcared the same way as the other, working fields, using functions like TextArea::make() and TextInput::make. anyone have an idea of why just some fields would possibly me skipped from initialization? my workaround fix works right now but it feels kinda hacky and it would be better if i could fix the problem from the root.Solution:Jump to solution
Even if they don’t need default data you still have to call formFill in mount.
10 Replies
Are both forms using the public $data property.?
no, i've declared different data objects for each of them
something like
public ?array $data1 = [];
public ?array $data2 = [];
public ?array $data3 = [];
etc.
and then used that variable name in the forms statePath optionHmm, I would expect it to be fine then. Can you share some code.
yeah let me see how i can make it readable 1 sec
ill just put one of the form that works and one that doesnt
something like this
i have them in separate files to be cleaner but i moved it all so it fits in 1 discord message
these are only the relevant parts of my .php file
and in the blade its just {{ $this->form1 }}
Are you calling formFill() for each one in your mount() method?
only for the ones that need some default data
these 2 don't have any default data
do i need to call it to initialize the $data anyway?
form1 has some data coming from the DB that i have to prefill in there which i did using formFill
Solution
Even if they don’t need default data you still have to call formFill in mount.
would just an empty one work?
$this->form3->fill();
? or should i specify all of the fieldsJust calling fill() should be enough.
It does a lot under the hood.
ah yeah that was definitely it, i looked again and actually form4 had a little bit of default data using fill too which is why it didnt have any problems like form3
i put it in for all them and it works perfectly now after removing the manual field i put in the $data3 variable
didn't realize fill does multiple things under the hood
thank you so much for the help 😄