Never done something like this, but there are some built-in options for actions like ->callAfterFormValidated() and ->afterFormValidated() that might be of help, as you can pass whatever function you want inside them. You can find out more here: https://filamentphp.com/docs/3.x/actions/prebuilt-actions/create#lifecycle-hooks and here: https://filamentphp.com/docs/3.x/actions/prebuilt-actions/create#halting-the-creation-process
Showing modal after validate form
I am using an action to create a model resource (I'm replacing the getCreateFormAction method in my CreateQuestion file). I want the action to validate first, then display a modal, and only when the user clicks "Submit" inside the modal, create the resource.
What's the problem then?
When I tried to do something like this:
use App\Filament\Resources\QuestionResource;
use Filament\Actions\Action;
use Filament\Resources\Pages\CreateRecord;
class CreateQuestion extends CreateRecord
{
protected static string $resource = QuestionResource::class;
protected function getCreateFormAction(): Action
{
return Action::make('create')
->label('Create')
->modalContent(function () {
$this->validate();
return view('hello-world');
})
->keyBindings(['mod+s']);
}
}
It just threw the validation exception but Filament didn't handle it (no red text fields or error messages appeared, it just threw the screen with the Laravel Ignition trace).
I also tried doing something like this but it shows the modal and then when I click the action inside the modal it validates the data.
//...
protected function getCreateFormAction(): Action
{
return Action::make('create')
->label('Create')
->action(fn () => $this->validate())
->modalContent(fn() => view('hello-world'))
->keyBindings(['mod+s']);
}
Other solution that just didn't work and the result was the same that before:
protected function getCreateFormAction(): Action
{
return Action::make('create')
->label('Create')
->after(fn () => $this->validate()) // Instead "action", i tried with after
->modalContent(fn() => view('hello-world'))
->keyBindings(['mod+s']);
}
What I'm doing wrong? I'm new using Filament.
Please or to participate in this conversation.