Throw validation (exception) from action
Can I throw an validation error / exception from an action and show it in the form?
Solution:Jump to 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',
]);...
7 Replies
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.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.
Can you move the content check to a custom validator on your upload field?
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
Not tested but I feel like something like this should work (in your action) :
Not sure about the key, because this is within a modal. But you can experiment with that.