F
Filament5mo ago
MZX

Disable the create button unless the terms and conditions checkbox is ticked?

Is there a way to disable the create button?
12 Replies
awcodes
awcodes5mo ago
What create button? We need more context.
MZX
MZXOP5mo ago
protected function getCreateFormAction(): Action
{
return Action::make('create')
->label('Pay')
->submit('create')
->color('success')
->disabled();

}
protected function getCreateFormAction(): Action
{
return Action::make('create')
->label('Pay')
->submit('create')
->color('success')
->disabled();

}
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
Forms\Components\Checkbox::make('agree_terms')
->label('Agree to terms and conditions')
->required(),
Forms\Components\Checkbox::make('agree_terms')
->label('Agree to terms and conditions')
->required(),
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
awcodes
awcodes5mo ago
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
MZX
MZXOP5mo ago
I tried this ->disabled(fn ($get) => !$get('agree_terms')); getting an error though
awcodes
awcodes5mo ago
What is the actual error?
MZX
MZXOP5mo ago
An attempt was made to evaluate a closure for [Filament\Actions\Action], but [$get] was unresolvable.
awcodes
awcodes5mo ago
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.
MZX
MZXOP5mo ago
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
awcodes
awcodes5mo ago
$get might not be injected into the action.
MZX
MZXOP5mo ago
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
awcodes
awcodes5mo ago
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.
MZX
MZXOP5mo ago
protected function getCreateFormAction(): Action
{
return Action::make('create')
->label('Pay')
->submit('create')
->color('success')
->disabled(function (Livewire $livewire)
{
dd();
});


}
protected function getCreateFormAction(): Action
{
return Action::make('create')
->label('Pay')
->submit('create')
->color('success')
->disabled(function (Livewire $livewire)
{
dd();
});


}
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?
Want results from more Discord servers?
Add your server