Search code examples
phplaravellaravel-5laravel-validation

Laravel Validation Same not Working as intended


I'm writing a Request Validator in Laravel, where I have the following conditions. To check if passwords given are the same, this will fail, with the message that the passwords are not the same, even though the passwords are the same.

'password1' => 'string',
'password2' => 'string|same: password1'

If I disable the validation and dd(Input::only('password1','password2'));, it will print out the following.

array:2 [
    "password1" => "123"
    "password2" => "123"
]

Why is the same validation not working?


Solution

  • Your issue is the space in your validation rule. You don't have a field named [space]password1 in your input, so the validation fails. Instead of same: password1, it should be same:password1.

    'password1' => 'string',
    'password2' => 'string|same:password1'
    

    Another way that password confirmation is usually done is with the confirmed validation. Typically you have a password field and a password_confirmation field, and then the confirmed validation will validate that your password input has matching input from a *_confirmation field.

    'password' => 'string|confirmed',
    'password_confirmation' => 'string',