Validate Form with array of rules.

Basically, I have a concrete class (SummerchildCareForm: https://codeshare.io/axgzWd) which build the schema or form fields with nested layout. This concrete class extends the base/abstract class which contains the traits and little common logic. For now I have only one SummerChildCareForm class which has a method getSchema, and also a static method "public static function validationRules(): array {return ['first_name' =>['required'...]...];}". I am rendering this form on the frontend side with Livewire, https://codeshare.io/2p7zbx The form get renders and when i hit the save changes button I even get the result but the validation doesn't work... I wanted to ask is there possibility to pass validation rules array to the form directly instead writing rules for in each field? Filamentphp v4 with Laravel v12.24
1 Reply
phoenix404dev
phoenix404devOP2mo ago
This is how seems to be resolved or a work around :(.
if ($validator->fails()) {
foreach ($validator->errors()->toArray() as $field => $messages) {
foreach ($messages as $message) {
$this->addError("data.{$field}", $message);
}
}
return;
}
if ($validator->fails()) {
foreach ($validator->errors()->toArray() as $field => $messages) {
foreach ($messages as $message) {
$this->addError("data.{$field}", $message);
}
}
return;
}
Submit method
$this->form->validate();
$data = $this->form->getState(); // already validate the data with default rules provided in the fields
$validator = Validator::make($data, $this->getRules());
if ($validator->fails()) {
foreach ($validator->errors()->toArray() as $field => $messages) {
foreach ($messages as $message) {
$this->addError("data.{$field}", $message);
}
}
return;
}

$validatedData = $validator->validated();
dd($validatedData);
$email = $validatedData['email'] ?? null;
$this->form->validate();
$data = $this->form->getState(); // already validate the data with default rules provided in the fields
$validator = Validator::make($data, $this->getRules());
if ($validator->fails()) {
foreach ($validator->errors()->toArray() as $field => $messages) {
foreach ($messages as $message) {
$this->addError("data.{$field}", $message);
}
}
return;
}

$validatedData = $validator->validated();
dd($validatedData);
$email = $validatedData['email'] ?? null;

Did you find this page helpful?