Validate Before Action Modal Renders

Does anyone know if there's a way to run validate your forms before the modal renders? In actions, schema() runs first before action(), however, I need my form to be validated first before the modal shows up. I could pass $livewire in a closure for schema(), and then do $livewire->validate(), but if a ValidationException occurs, it is not handled the same way as when a ValidationException occur inside action(). If the exception occurs inside action (assuming we don't have a modal), the fields that failed the validation gets highlighted red. But if it as inside schema(), an error log just pops up. This is my current solution:
->schema(function (Page $livewire) {
static $notificationSent = false;

if (! self::validate($livewire)) {
if (! $notificationSent) {
// Notification object is sent
$notificationSent = true;
}
return null;
}
$notificationSent = false;

return self::getModalSchema($livewire);
})

---

private static function validate(Page $livewire): bool
{
try {
$livewire->validate();
} catch (ValidationException $exception) {
// I can't throw it or else the error log pops up
return false;
}
return true;
}
->schema(function (Page $livewire) {
static $notificationSent = false;

if (! self::validate($livewire)) {
if (! $notificationSent) {
// Notification object is sent
$notificationSent = true;
}
return null;
}
$notificationSent = false;

return self::getModalSchema($livewire);
})

---

private static function validate(Page $livewire): bool
{
try {
$livewire->validate();
} catch (ValidationException $exception) {
// I can't throw it or else the error log pops up
return false;
}
return true;
}
- I am using a notification for now as an indicator that the validation failed - I would prefer if the fields get highlighted red when a ValidaitonException occurs - I have a notificationSent flag cause for some reason, schema() is called multiple times in quick succession when I click the action. Context: I don't have much experience with Laravel, Livewire, Filament.
Solution:
Just add: $livewire->setErrorBag($exception->validator->errors()); inside of the catch block. ```php private static function validate(Page $livewire): void {...
Jump to solution
2 Replies
Solution
BerriesAndCream
BerriesAndCream2mo ago
Just add: $livewire->setErrorBag($exception->validator->errors()); inside of the catch block.
private static function validate(Page $livewire): void
{
try {
$livewire->validate();
} catch (ValidationException $exception) {
$livewire->setErrorBag($exception->validator->errors());
}
}
private static function validate(Page $livewire): void
{
try {
$livewire->validate();
} catch (ValidationException $exception) {
$livewire->setErrorBag($exception->validator->errors());
}
}
BerriesAndCream
BerriesAndCreamOP2mo ago
It stopped working for some reason...

Did you find this page helpful?