Conditionally Prevent Record Edit?
I know I can disable form inputs one-by-one via
Thanks in advance for any guidance!
->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
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:
Then on the View page I remove the edit button conditionally (again)
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
Sounds like this would be a better use case in a policy.
Policy? hmmm didn't even occur to me.
As in a model policy?
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.
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! rocks almost as much as you do! Two for one...wahooo!