F
Filament15mo ago
Tanuki

BeforeValidation addError on Wizard Step with repeater

Hi, I'm using the Filament\Forms seperated from admin panel. I'm trying to add additional verification on a step component. This step component contains a repeater leaves that has items in it. I'm trying to add an error on the step. I don't know what I'm doing wrong. I'd appreciate if anyone could help. Also my form data is stored in public array $data;
Forms\Components\Wizard\Step::make('absence(s)')

->beforeValidation(function (array $state, CreateLeaveForm $livewire, Forms\Components\Wizard\Step $component) {


foreach ($state['leaves'] as $key => $leave) {
$livewire->addError("leaves.$key.from_date", 'An error message here.');
}

});
Forms\Components\Wizard\Step::make('absence(s)')

->beforeValidation(function (array $state, CreateLeaveForm $livewire, Forms\Components\Wizard\Step $component) {


foreach ($state['leaves'] as $key => $leave) {
$livewire->addError("leaves.$key.from_date", 'An error message here.');
}

});
1 Reply
Tanuki
TanukiOP15mo ago
The solution is to to do :
$errors = [];

foreach ($state['leaves'] as $key => $leave) {
$errorKey = "data.leaves.$key.from_date";
$errorMessage = 'An error message here.';

$livewire->addError($errorKey, $errorMessage);
$errors[$errorKey] = [$errorMessage];
}

if (!empty($errors)) {
throw \Illuminate\Validation\ValidationException::withMessages($errors);
}
$errors = [];

foreach ($state['leaves'] as $key => $leave) {
$errorKey = "data.leaves.$key.from_date";
$errorMessage = 'An error message here.';

$livewire->addError($errorKey, $errorMessage);
$errors[$errorKey] = [$errorMessage];
}

if (!empty($errors)) {
throw \Illuminate\Validation\ValidationException::withMessages($errors);
}
Want results from more Discord servers?
Add your server