Skip to content

Commit 0ef950b

Browse files
johndivamMohammad Salamalexjustesen
authored
Improvements user resource (#1732)
* Improve textInput of password,password_confirmation * Improve textInput of password,password_confirmation * Drop the label if the field --------- Co-authored-by: Mohammad Salam <[email protected]> Co-authored-by: Alex Justesen <[email protected]>
1 parent bc56830 commit 0ef950b

File tree

2 files changed

+13
-20
lines changed

2 files changed

+13
-20
lines changed

app/Filament/Resources/UserResource.php

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,23 +43,15 @@ public static function form(Form $form): Form
4343
Forms\Components\TextInput::make('password')
4444
->required()
4545
->password()
46-
->dehydrateStateUsing(fn ($state) => Hash::make($state))
47-
->visible(fn ($livewire) => $livewire instanceof CreateUser)
48-
->rule(Password::default()),
49-
Forms\Components\TextInput::make('new_password')
50-
->password()
51-
->label('New Password')
52-
->nullable()
53-
->rule(Password::default())
54-
->visible(fn ($livewire) => $livewire instanceof EditUser)
55-
->dehydrated(false),
56-
Forms\Components\TextInput::make('new_password_confirmation')
46+
->required(fn (string $context) => $context === 'create')
47+
->nullable(fn (string $context) => $context === 'edit')
48+
->maxLength(60),
49+
Forms\Components\TextInput::make('password_confirmation')->label('Confirm Password')
50+
->required()
5751
->password()
58-
->label('Confirm New Password')
59-
->rule('required', fn ($get) => (bool) $get('new_password'))
52+
->required(fn (string $context) => $context === 'create')
53+
->nullable(fn (string $context) => $context === 'edit')
6054
->same('new_password')
61-
->visible(fn ($livewire) => $livewire instanceof EditUser)
62-
->dehydrated(false),
6355
])
6456
->columns(1)
6557
->columnSpan([

app/Filament/Resources/UserResource/Pages/EditUser.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@ protected function getHeaderActions(): array
1818
];
1919
}
2020

21-
public function beforeSave()
21+
protected function mutateFormDataBeforeSave(array $data): array
2222
{
23-
if (! array_key_exists('new_password', $this->data) || ! filled($this->data['new_password'])) {
24-
return;
25-
}
23+
if (!isset($data['password']) || empty( $data['password'])) {
24+
unset($data['password']);
25+
}
2626

27-
$this->record->password = Hash::make($this->data['new_password']);
27+
return $data;
2828
}
29+
2930
}

0 commit comments

Comments
 (0)