F
Filament15mo ago
Johan

Throw validation (exception) from action

Can I throw an validation error / exception from an action and show it in the form?
Solution:
Not tested but I feel like something like this should work (in your action) : ```php throw ValidationException::withMessages([ 'data.my_upload_field' => 'Custom error message here', ]);...
Jump to solution
7 Replies
Patrick Boivin
Patrick Boivin15mo ago
Maybe... but what's the context? If it's an actual validation thing, you can call $livewire->form->getState() to trigger the form validation as part of the action.
Johan
JohanOP15mo ago
In the table header I have an import button which triggers the modal with some form fields. In the form there is an upload field, the action checks the content of the file. If it is not good I want an error message on the upload field.
Patrick Boivin
Patrick Boivin15mo ago
Can you move the content check to a custom validator on your upload field?
Johan
JohanOP15mo ago
True but I use the Maatwerk Excel package to validate it. To be consistent with the frontend. So I would like to forward the errors of that package to the modal form of the action.
Solution
Patrick Boivin
Patrick Boivin15mo ago
Not tested but I feel like something like this should work (in your action) :
throw ValidationException::withMessages([
'data.my_upload_field' => 'Custom error message here',
]);
throw ValidationException::withMessages([
'data.my_upload_field' => 'Custom error message here',
]);
Patrick Boivin
Patrick Boivin15mo ago
Not sure about the key, because this is within a modal. But you can experiment with that.
Want results from more Discord servers?
Add your server