2

I am trying to check if entered password is currently logged in users password and give error message if not.

I have profile editing form with name, email etc.

Last input in form is password input.

How can I check if password that is entered is current password?

In EditProfileRequest i tried (custom request file, made with artisan):

  public function rules() {
    return [
        'first_name' => 'required|max:255',
        'email' => 'required|email|max:255|unique:users,email,'.Auth::user()->id,
        'country_code' => 'digits_between:1,4|numeric',
        'phone' => 'digits_between:5,12|numeric',
        'password' => 'required|confirmed',
    ];
}

but it rejected form with correct password. Then I tried adding to controller this part:

if (!Hash::check(Input::get('password'), Auth::user()->password)) {
        return Redirect::back()->withInputs();
    }

this worked but I don't get any errors for user and I don't know how to add custom error messages, since I am using built-in validator.

1
  • So at the end I used: if (!Hash::check(Input::get('password'), Auth::user()->password)) { return Redirect::back()->withErrors(['password' => 'Incorrect password']); }
    – Riiwo
    Feb 10, 2016 at 21:36

2 Answers 2

2

You can pass the custom error message as following.

$validator = Validator::make(Input::all(), $rules);
//Here you are checking your inputs with the validation rules that you are created. 

if ($validator->fails())
{
    //If the validation fails, check for the password validation too and return the error. 
    if (!Hash::check(Input::get('password'), Auth::user()->password)) {
        return Redirect::back()
                          ->withInputs()
                          ->withErrors(['login' => 'Your custom Error Msg'])
                          ->withErrors($validator);
    }
    else{
               return Redirect::back()
                          ->withInputs()
                          ->withErrors(['login' => 'Your custom Error Msg']);
   }
}

As a short answer for your question to get a custom error message:

return Redirect::back()->withInputs()->withErrors(['login'=>'custom msg']);
7
  • I removed 'withErrors($validator)' and 'withInputs()' and changed "['login', 'Your custom Error Msg']" to "['login' => 'Your custom Error Msg']" and got it working, also all other inputs were correct (ones before changing).
    – Riiwo
    Feb 10, 2016 at 21:14
  • Why should the OP "try this"? A good answer will always have an explanation of what was done and why it was done that way, not only for the OP but for future visitors to SO. Feb 10, 2016 at 21:17
  • @JayBlanchard: I can understand, but I always edit my answers later after the initial submit. Feb 10, 2016 at 21:20
  • I agree, I would like to have explanation too, like does this Validator::make overwrite the builtin one and also where should People put it.
    – Riiwo
    Feb 10, 2016 at 21:20
  • Validation::make is not overwriting anything. It's just performing the validation with the rules that you created for the particular model. Feb 10, 2016 at 21:23
0

Also make sure you display the errors in your login page.

@if (count($errors) > 0)
<div class="alert alert-danger">
    <ul>
        @foreach ($errors->all() as $error)
            <li>{{ $error }}</li>
        @endforeach
    </ul>
</div>
@endif

You can check displaying validation errors on this page https://laravel.com/docs/5.2/validation#quick-displaying-the-validation-errors

4
  • This is not login page, but profile editing form as said in question description. And I have this part implemented, thanks.
    – Riiwo
    Feb 10, 2016 at 21:24
  • I see. Does your form has the csrf_token field?
    – Can Celik
    Feb 10, 2016 at 21:30
  • Yes, got it working. (See Jilson Thomas answer and my comment on question)
    – Riiwo
    Feb 10, 2016 at 21:37
  • Glad it's working. I actually wanted to comment on that answer but my reputation points is not enough to do it. So trying to help more people now :)
    – Can Celik
    Feb 10, 2016 at 21:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.