I try to check the password field only if it is present. I want to allow someone to edit the user, and they may or may not want to change the user password. Therefore, I thought that I could use the Laravels validation rules, in particular the "sometimes" rule. I have this rule set:
$this->validate($request, [ 'password' => 'sometimes|required|min:8', ]);
This is simplified for example, usually there will be different rules for other fields and more stringent rules for a password. I expect this to apply the min: 8 rule only if the password field is present in the transmitted data, but if I leave the password field empty, I will receive a validation error indicating that a password field is required.
I am not sure what I do not understand in the docs. Do I need to manually delete the password field before validation, if it was entered in the form of a form like this?
$data = $request->all(); if ('' === $data['password']) { unset($data['password']) }
... and then pass the array to the validator. I think this makes sense, but I can make with some confirmation that I understand correctly. Thanks in advance.
php validation laravel
Steven1978
source share