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:
Even if they don’t need default data you still have to call formFill in mount.
Jump to solution
10 Replies
awcodes
awcodes5d ago
Are both forms using the public $data property.?
Ryuuuu
RyuuuuOP5d ago
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 option
awcodes
awcodes5d ago
Hmm, I would expect it to be fine then. Can you share some code.
Ryuuuu
RyuuuuOP5d ago
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
protected function getForms(): array
{
return [
'form1',
'form2',
'form3',
'form4',
'form5',
];
}
/**
* @var array<string, mixed> | null
*/
public ?array $data3 = ["note" => ""];

/**
* @var array<string, mixed> | null
*/
public ?array $data4 = [];

public function form3(Form $form3): Form
{
return $form3->schema([
Section::make('Section1')
->schema([
Select::make('level')
->label('Level')
->required()
->options([
'draft' => 'Draft',
'reviewing' => 'Reviewing',
'published' => 'Published',
]),
Textarea::make('note')
->label('Note')
->placeholder('notes')
->nullable(),
])
->columns(1)
->collapsible()
])
->statePath('data3');
}

public function form4(Form $form4): Form
{
return $form4->schema([
Section::make('Section1')
->schema(
[
TextInput::make('input1')
->label('Input1')
->integer()
->required(),
TextInput::make('input2')
->label('Input2')
->integer()
->required(),
]
)
->collapsible()
])
->statePath('data4');
}
protected function getForms(): array
{
return [
'form1',
'form2',
'form3',
'form4',
'form5',
];
}
/**
* @var array<string, mixed> | null
*/
public ?array $data3 = ["note" => ""];

/**
* @var array<string, mixed> | null
*/
public ?array $data4 = [];

public function form3(Form $form3): Form
{
return $form3->schema([
Section::make('Section1')
->schema([
Select::make('level')
->label('Level')
->required()
->options([
'draft' => 'Draft',
'reviewing' => 'Reviewing',
'published' => 'Published',
]),
Textarea::make('note')
->label('Note')
->placeholder('notes')
->nullable(),
])
->columns(1)
->collapsible()
])
->statePath('data3');
}

public function form4(Form $form4): Form
{
return $form4->schema([
Section::make('Section1')
->schema(
[
TextInput::make('input1')
->label('Input1')
->integer()
->required(),
TextInput::make('input2')
->label('Input2')
->integer()
->required(),
]
)
->collapsible()
])
->statePath('data4');
}
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 }}
awcodes
awcodes5d ago
Are you calling formFill() for each one in your mount() method?
Ryuuuu
RyuuuuOP5d ago
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
awcodes
awcodes5d ago
Even if they don’t need default data you still have to call formFill in mount.
Ryuuuu
RyuuuuOP5d ago
would just an empty one work? $this->form3->fill();? or should i specify all of the fields
awcodes
awcodes5d ago
Just calling fill() should be enough. It does a lot under the hood.
Ryuuuu
RyuuuuOP5d ago
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 😄
Want results from more Discord servers?
Add your server