Conditionally disabling form fields

Hi all! I'm a beginner in Laravel and quite new on Filament, and I'm trying to conditionally disable some form fields on certain conditions. I've been using the ->disabledOn() method in form fields to disable them on edit page or create page, for example, but I now need to disable a FileUpload form in the Edit view only if it already has a file, and I have no clue of how to do this. Any push in the right direction would be a bless. I'm using Spatie Media Library and Shield Plugins (Maybe in the near future I have to conditionally disable a form field based on the user role) Thanks in advance!
4 Replies
awcodes
awcodes11mo ago
Just use disabled() and you can use $record and $context in the callback to test both conditions to return true or false.
Esteban Ojeda
Esteban Ojeda11mo ago
Thanks @awcodes ! I found out that it works using
->disabled(fn (Get $get):bool => filled($get('file')))
->disabled(fn (Get $get):bool => filled($get('file')))
But I think yours would be an optimal solution. Could you provide an example?
awcodes
awcodes11mo ago
Same thing you’re doing now. Just add $context after $get, then && $context === ‘edit’ after the filled() Sorry on my phone, not easy to type code examples.
Esteban Ojeda
Esteban Ojeda11mo ago
Made it! Thank you a lot!