Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

peterhrobar's avatar

Close confirmation modal on validation error

I have a save form action defined for my Filament wizard like this in the EditConfiguration.php file:

protected function getSaveFormAction(): Action
    {
        return Action::make(__('Save'))
            ->requiresConfirmation()
            ->modalDescription(__('Are you sure you\'d like to save the configuration: ":configurationName"?', ['configurationName' => $this->data['name']]))
            ->action(fn() => $this->save());
    }

If the form runs into a validation error then I'm sending a notification to the end-user using the onValidationError method:

protected function onValidationError(ValidationException $exception): void
    {        
        Notification::make()
            ->title($exception->getMessage())
            ->danger()
            ->send();
    }

I would also like to close the confirmation modal in case of a validation error occurs since this would provide the best UX. Is there a simple way to accomplish that with Filament?

1 like
8 replies
peterhrobar's avatar

Something like this:

return Action::make(__('Save'))
       ->requiresConfirmation()
       ->modalDescription(__('Are you sure you\'d like to save the configuration: ":configurationName"?', ['configurationName' => $this->data['name']]))
       ->action(function(Action $action) {
           try {
               $this->validate();
               $this->save();
           } catch (\Exception $e) {
               (property_exists($e, 'validator')) &&
               ($e?->validator instanceof Validator) &&
                   $this->setErrorBag($e->validator->errors());
               $action->cancel();
           }
       });

You need to set the error bag if you do the validation manually ...

KhalidCodes's avatar

Just putting this here in case somebody needs it.

$this->closeActionModal();

Did the trick for me. Hope this helps.

DevotedZest's avatar

I totally get why you'd want to close the confirmation modal on a validation error—it makes for a smoother user experience. In Filament, one way to handle this is by manually closing the modal using $this->dispatch('closeModal') if validation fails. You can add this inside your validation logic.

That said, if you're using Filament’s built-in form validation inside a modal, it usually keeps the modal open when validation fails. If you're handling validation manually, you might need to trigger the modal close event explicitly.

Hope that helps! Let me know if you need an example.

KhalidCodes's avatar

@DevotedZest It's $this->dispatch('close-modal', id: 'modal-id-goes-here') you still need the modal id, which I wasn't able to get from my custom page. $this->closeActionModal() does the job tho.

beefsoupclover's avatar

@DevotedZest, I would love an example if you have one. I couldn't get the validation to happen before the modal. I did this work around, but maybe not the cleanest way?

1 like
orpheusohms's avatar

Here is another way. I decided to show instant feedback on the input pre form validation. So with this you get instant feedback on the input.

Mix & Match

Additionally you can also use the disabled method to disable the action if the form is invalid. That way you have instant feedback on the input.

Action::make('updateWorkspace')
    ->label('Update')
    ->action(function () {
        $this->updateWorkspace('general_information');
    })
    ->requiresConfirmation()
    ->disabled(function () {
        try {
            $this->form->validate(); // @phpstan-ignore-line
            return false;
        } catch (ValidationException $e) {
            $this->setErrorBag($e->validator->errors());
            return true;
        }
    })
    ->modalDescription('Are you sure you want to update this workspace information?'),

Please or to participate in this conversation.