Making system-wide changes to `->required()`
I have a system with some imported data. Some of these fields in the old data are empty, but required in the new system. This causes problems when editing one of these older entries.
I want to allow admins to edit these entries anyway, something like this:
Now, this would be annoying to do for every single field I have. How can I make this change system-wide?
Thanks!
8 Replies
I would make a custom laravel validation rule (required_unless_admin or sth) like this: https://laravel.com/docs/10.x/validation#custom-validation-rules
And then implement it like this:
https://filamentphp.com/docs/3.x/forms/validation#custom-rules
Maybe you could also use this: https://filamentphp.com/docs/3.x/forms/validation#required-unless
Laravel - The PHP Framework For Web Artisans
Laravel is a PHP web application framework with expressive, elegant syntax. We’ve already laid the foundation — freeing you to create without sweating the small things.
you can just put it inside the boot it in the
AppServiceProvider
whatever inputs you have:
Ah even better! didnt know that!
Ah yes i want to watch that series, now busy with the intro course. Lot of nice little things i overlooked. Thanks for the recommendation!
@ja Thanks for the suggestion! If I understand correctly, this would require all optional fields as well (for non-admins). That would be problematic as well...
I might just add a macro to wrap the logic and call that instead of
->required()
, but that's not very ideal for DX as I'll have to remember to use the custom macro instead of ->required()
Create a custom form field like AdminTextInput.
Also not ideal.. I'll have to make one for every field type I use. And I have to remember to use the custom input instead of the default one. I just went with the macro until I find something better
Thanks for the help everyone!