2

So I am receiving data from a form that should reset user passwords:
Old Password: |field|
New Password: |field|
Confirm Password: |field|
And i want to be able to display a message out for the user if his old password does not match what he entered in the first field. I don't want to make an entirely new validation method and just want to throw an error to the use when i make my own if(). So how do I achieve this using the $errors variable that is available in my blade views

So here is an example of my controllers method

public function update(Request $request){
    $this->validate($request,[
        'oldPassword' => 'required',
        'password' => 'required|min:8|confirmed'
    ]);
    $user = Auth::user();
    if(password_verify($request->newPass,$user->password)){
         $user = User::find($user->id);
         $user->password = bcrypt($request->newPass);
         $user->save();
    }else{
         //the code for adding a new key to $errors variable
         return back(); Or return redirect('path');
    }
}

So in the view I want to this

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

1 Answer 1

5

You can do this in your controller:

$validator =  Validator::make($request->all(),[
    'oldPassword' => 'required',
    'password' => 'required|min:8|confirmed'
]);

And then before your return back();, add:

$validator->after(function($validator) {
    $validator->errors()->add('tagName', 'Error message');
});

With your message.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.