F
Filament8mo ago
Susan

Make the field required only on creation.

I have user form with field password. I need to make the form mandatory only if it is in creation state. On updation, the field should be non-mandatory. TextInput::make('password') ->password() ->dehydrateStateUsing(fn ($state) => Hash::make($state)) ->dehydrated(fn ($state) => filled($state)) ->required(fn (Page $livewire) => ($livewire instanceof CreateUser) ? false : true), This is what I h ave given, please advice me how to make it working. Thanks
Solution:
->required(fn (string $context) => $context === 'create') This is working for me. Thank you....
Jump to solution
4 Replies
MohamedSabil83
MohamedSabil838mo ago
You switch between false and true. Your code says make it always required but in CreateUser. Make it simple ->required(fn (Page $livewire) => $livewire instanceof CreateUser). Another way, use this ->required(fn (string $context) => $context === 'create')
Solution
Susan
Susan8mo ago
->required(fn (string $context) => $context === 'create') This is working for me. Thank you.
MohamedSabil83
MohamedSabil838mo ago
You're welcome. Don't forget to mark it as a solution.
Susan
Susan8mo ago
Yeah sure.