Disable the create button unless the terms and conditions checkbox is ticked?
Is there a way to disable the create button?
12 Replies
What create button? We need more context.
So this is my custom create button for a resource (On the createResource page)
and on the main Resourcepage, inside the form I have this
I just want to know the conditional logic inside the disabled() for it to be enabled when the checkbox is ticked
Also, the agree_terms doesn't exist in the model/migration, it's just for namesake
So you need to use a callback in your createAction()->action() to check the form’s state for the checkbox. https://filamentphp.com/docs/3.x/forms/advanced
I tried this
->disabled(fn ($get) => !$get('agree_terms'));
getting an error though
What is the actual error?
An attempt was made to evaluate a closure for [Filament\Actions\Action], but [$get] was unresolvable.
Ok, so go higher up $livewire is injected so you should be able to get the data off of that.
All pages in panels are the livewire component so the state is always available there.
Sorry I don't get you, why isn't the $get resolvable though. I am just trying to access the state of the agree_terms. It should work in theory
$get might not be injected into the action.
Oh so it only works for the fields
What's the alternative then? Could you mind showing me how it's done just to get an idea
The same way you are trying to inject $get. You can inject $livewire. The get the data off of it.
Just inject $livewire into the action callback and dd() to see what you have access to.
Or the livewire component is always available off of the action itself. $action->getLivewire() something like that.
Ray() is probably better than dd() in these cases though. But source diving will go a long way.
i did this, and the dd shows i have access to the createResource page
Not the main ResourcePage
The create action is on the createResource page, while the form is on the ResourcePage, so I guess that's the issue?