Mar 30, 2024
0
Level 1
How to validate array fields once?
Hi guys, I have this:
public ?array $fieldsData = [];
public function mount()
{
$this->fieldsData = [
'first_name' => '',
'last_name' => '',
'country' => '',
];
}
...
->schema([
Forms\Components\TextInput::make('first_name')
->placeholder('First name')
->hiddenLabel()
->required(),
...
Forms\Components\Actions::make([
Forms\Components\Actions\Action::make(__('Make Payment'))
->action(function (Forms\Get $get, Forms\Set $set, $livewire, Component $component) {
//$livewire->validateOnly($component->getStatePath()); // NOT working!
$fieldsArrayName = $component->getStatePath();
// validate all fields, if any failed it will stop so never call submit()
foreach($this->{$fieldsArrayName} as $field => $value)
{
$this->validateOnly($fieldsArrayName.'.'.$field);
}
// success: all field are valid
$this->submitBuyPhoneNumber();
})
]),
])->statePath('fieldsData'),
I use a Filament Wizard and at the last step I use a custom submit button (action button).
My research showed me that I could use $livewire->validateOnly('fieldsData'); or $this->validateOnly('fieldsData'); to validate the fields but it doesn't work, it always passes validation regardless of whether the field is valid or not (returning an empty array).
The only thing that works is to validate them one by one, as I've done here.
I'm pretty sure there should be a way to validate them only once. I'm just improving my knowledge...
Please or to participate in this conversation.