F
Filament16mo ago
Finn

Required but only on create?

Is there a way to set a field to only be required on a create action? For example when editing a user - on create they require a password to be entered but on update they do not always need to update their password.
Solution:
The required method accepts a callback and you can inject the current operation into that callback:
->required(fn (string $operation): bool => $operation === 'create')
->required(fn (string $operation): bool => $operation === 'create')
...
Jump to solution
6 Replies
toeknee
toeknee16mo ago
->required(fn($context) => $context === 'create')
Solution
ConnorHowell
ConnorHowell16mo ago
The required method accepts a callback and you can inject the current operation into that callback:
->required(fn (string $operation): bool => $operation === 'create')
->required(fn (string $operation): bool => $operation === 'create')
ConnorHowell
ConnorHowell16mo ago
Pretty sure it's 'operation' and not 'context'?
toeknee
toeknee16mo ago
Could be in V3... not fully on it yet
Finn
FinnOP16mo ago
worked perfectly, thank you. 🙂
awcodes
awcodes16mo ago
$context still works in v3 for legacy. But $operation is correct.

Did you find this page helpful?