php - Laravel 5.4 add a message to validator's errors -
so receiving data form should reset user passwords:
old password: |field|
new password: |field|
confirm password: |field|
, want able display message out user if old password not match entered in first field. don't want make entirely new validation method , want throw error use when make own if(). how achieve using $errors variable available in blade views
so here example of 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 adding new key $errors variable return back(); or return redirect('path'); } }
so in view want
@if (count($errors) > 0) <div class="alert alert-danger"> <ul> @foreach ($errors->all() $error) <li>{{ $error }}</li> @endforeach </ul> </div> @endif
you can in controller:
$validator = validator::make($request->all(),[ 'oldpassword' => 'required', 'password' => 'required|min:8|confirmed' ]);
and before return back();
, add:
$validator->after(function($validator) { $validator->errors()->add('tagname', 'error message'); });
with message.
Comments
Post a Comment