0

I am trying to add new error message in my validation if the user failed to input correct current password. Here is the code i am working on:

  $validator = $this->validate($request, [
        'current-password' => 'required',
        'password' => 'required|string|min:6|confirmed',
    ]);


  if (!(Hash::check($request->get('current-password'), Auth::user()->password))) {
      $validator->errors()->add('current-password', 'Your current password does not matches with the password you provided. Please try again.');
    }

I am getting this error when i submit the form: Call to a member function errors() on array

Any help would be appreciated. Thanks.

2 Answers 2

1

Try this

$this->validate($request, [
        'current-password' => 'required',
        'password' => 'required|string|min:6|confirmed',
        ], [
           'current-password.required' => 'your custom message',
           'password.required' => 'your custom message',
           'password.confirmed' => 'your custom message'
        ]);
Sign up to request clarification or add additional context in comments.

2 Comments

i am trying to add error message to $errors->all() in my view file if the user submitted a wrong password
the code you provided only handles error if there is no password and current-password submitted and if the confirmed password does not match the new password. Anyway, i already found a solution to my problem. I really appreciate your help bro.
1

i finally found a solution.

$validator = Validator::make(
    Input::all(),
    array(
            'current-password' => 'required',
            'password' => 'required|string|min:6|confirmed',
    )
);

if ($validator->fails())
{
    return Redirect()->route('customer.profile')->withErrors($validator)->withInput();
}

if (!(Hash::check($request->get('current-password'), Auth::user()->password))) {
    // The passwords matches
    $validator->getMessageBag()->add('current-password', 'Your current password does not matches with the password you provided. Please try again.');
     return Redirect()->route('customer.profile')->withErrors($validator)->withInput();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.