Conditionally Prevent Record Edit?

I know I can disable form inputs one-by-one via ->disabled(...). But, what if I wish to prevent editing for entire page based on a field in the model. Like if $record->is_locked == true ? If would be great to prevent the edit page from opening or in the least open to a view page.
Thanks in advance for any guidance!
5 Replies
bionary
bionaryOP2y ago
Well, I did it but it's very laborious as I needed to put the same logic in 5 different places. In the resource I'm disabling the "edit" link at the end of each table row via:
->actions([
Tables\Actions\EditAction::make('edit')->visible(fn($record) => ! $record['is_approved']),
])
->actions([
Tables\Actions\EditAction::make('edit')->visible(fn($record) => ! $record['is_approved']),
])
Then on the View page I remove the edit button conditionally (again)
protected function getActions(): array {
if($this->record->is_approved){
return [];
}else{
return [
EditAction::make(),
];
}
}
protected function getActions(): array {
if($this->record->is_approved){
return [];
}else{
return [
EditAction::make(),
];
}
}
Then just in case somebody navigates to the edit page. (people use back button, browser history, etc.) I have to remove the buttons on Edit
//Remove top buttons if record is_approved
protected function getActions(): array {
if($this->record->is_approved){
return [];
}else{
return [
Actions\ViewAction::make(),
Actions\DeleteAction::make(),
];
}
}
//Remove bottom buttons if record is_approved
protected function getFormActions(): array {
if($this->record->is_approved){
return [];
}else{
return parent::getFormActions();
}

}
//Remove top buttons if record is_approved
protected function getActions(): array {
if($this->record->is_approved){
return [];
}else{
return [
Actions\ViewAction::make(),
Actions\DeleteAction::make(),
];
}
}
//Remove bottom buttons if record is_approved
protected function getFormActions(): array {
if($this->record->is_approved){
return [];
}else{
return parent::getFormActions();
}

}
awcodes
awcodes2y ago
Sounds like this would be a better use case in a policy.
bionary
bionaryOP2y ago
Policy? hmmm didn't even occur to me. As in a model policy?
awcodes
awcodes2y ago
Yea. Or possibly in canEdit() method on the resource if you need to return a view or notification as to why they can’t edit the record.
bionary
bionaryOP2y ago
The policy idea works just fine. So elegant, thank you. 3 lines really. I didn't realize that the policy would "trickle down" and affect everything in filament...the action at the end of the table, the pages, buttons. Very slick! My only concern is the blasted shield plugin. that thing is a bear and I always seem to have to run shield:install --fresh for some reason after a while of adding/removing things. I think the policy might get overwritten at that point. I'll blow it all up and rebuild from scratch and see if that policy modification stays put. I just tried canEdit() and it too works perfectly! filament rocks almost as much as you do! Two for one...wahooo!
Want results from more Discord servers?
Add your server