Ronald Torres
Ronald Torres

Reputation: 199

Adding error message manually in Laravel Validation

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.

Upvotes: 0

Views: 1465

Answers (2)

Ronald Torres
Ronald Torres

Reputation: 199

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();
}

Upvotes: 1

Deepak
Deepak

Reputation: 624

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'
        ]);

Upvotes: 1

Related Questions