Hashing password fields and handling password updates
You have a password field:
use Filament\Forms\Components\TextInput; TextInput::make('password') ->password()
And you wanted to hash the password when the form is submitted:
use Filament\Forms\Components\TextInput;use Illuminate\Support\Facades\Hash; TextInput::make('password') ->password() ->dehydrateStateUsing(fn ($state) => Hash::make($state))
But you don't want to overwrite the existing password if the field is empty:
use Filament\Forms\Components\TextInput;use Illuminate\Support\Facades\Hash; TextInput::make('password') ->password() ->dehydrateStateUsing(fn ($state) => Hash::make($state)) ->dehydrated(fn ($state) => filled($state))
However, you want to require the password to be filled on the Create page of an admin panel resource:
use Filament\Forms\Components\TextInput;use Filament\Pages\Page;use Illuminate\Support\Facades\Hash; TextInput::make('password') ->password() ->dehydrateStateUsing(fn ($state) => Hash::make($state)) ->dehydrated(fn ($state) => filled($state)) ->required(fn (string $context): bool => $context === 'create')
Nice one :) But there's a small typo, the last line should be:
Thanks, I fixed it :)
@wakota, Its good, but if the resource is
--simple
,Page
is not valid, so the @dan suggestion is better in this caseUsing v3 the last line should be ->required(fn ($livewire) => ($livewire instanceof CreateRecord))
Changed my auth table to our employee table/resource. Implemented the above on the password field. Was having trouble with this one. Using the above, the password field was populated when editing a user. Assume this caused the contents to get rehashed? Used the below to leave the password blank when populating the edit form...
Not sure if this is the correct way to do it but it works fine now.
Thanks it works