sampang's avatar

Laravel Validation

Hey Folks, I am trying to make a form request named UpdateProfileRequest and validating my request there. I have my password validation as :

'password' => ['sometimes', 'min:6', 'confirmed']

all i wanted to do is if the password filed is filed then only update the password field otherwise do nothing to password field. It seems the request has password field as null if nothing is set from the form. I have tried doing this:

protected function prepareForValidation()  { 
  if ($this->password === null) { 
 		$this->request->remove('password');
  	}
 }     

It works when i send nothing from the form but doesn't work when i send something. It throws confirmation validation error, however i have set the equal values for password and password_confirmation .

Is there any other method to do this ? if so then please share .

0 likes
4 replies
sampang's avatar

@Nakov thank you for you answer but . password can't be null. that's why.

Nakov's avatar
Nakov
Best Answer
Level 73

@sampang you are asking for an update request. So the nullable rule will ignore the field if there is nothing entered.. if there is something in the field the min:6 and the confirmed rules will apply. You can try it :)

sometimes it is pretty much the same, except that when it exists in the sent request it will always require for a value.

The password cannot be null but for a CREATE request, when you first create a user you should not use the nullable but if the password is already set, UPDATE should allow for it to be null because it won't update it.

That's why I said use the validated() data, which will update just the fields that had change/new value.

Please or to participate in this conversation.