如何验证重置密码?

时间:2017-08-21 07:45:03

标签: php laravel

请解决我的问题。控制器页面如下例所示:

Controller.php这样

public function TeacherUpdatePassword(Request $request)
{ 
 $this->validate($request, [
    'curr_password'   => 'required',
    'new_password'    => 'required|min:4',
    'confm_password'  => 'required|same:new_password',],

    [
    'curr_password.required'   => 'Enter Current Password',
    'new_password.required'    => 'required|min:4',
    'confm_password.required'  => 'required|same:new_password',
]);

 if(!Hash::check($curr_password,Auth::user()->password)){
\Session::flash('success', 'The specified password does not match.'); 
 return Redirect('/studentresetpassword');
}

else if ($new_password !== $confm_password) {
\Session::flash('success', 'Passwords are NOT the same.'); 
 return Redirect('/studentresetpassword');
}

else{
$request->user()->fill(['password' => Hash::make($new_password)])->save();
\Session::flash('success', 'Updated Successfully.');   
 return Redirect('/studentresetpassword');
}

}

Teacherresetpassword.blade.php

@extends('layouts.default')
@section('content')
<div id="profileTeacher" type="view" class="demo-section k-header">
@include('layouts.common.flash-message')

   {!! Form::open(['url' => 'teacherupdatepassword',  'method' => 'post'])  !!} 
<form id="profileTeacherForm" method="post" action="" >
    <ul id="fieldlist" >
            <li>
            <label style="color:Green;font-size:15px">Update Password</label>
            </li> 
            <li>
             <table id="lessonPlanTable">

                 <tr>
                  <td><label> Current Password  </label> </td>
                  <td><input type="password" id="curr_password" name="curr_password"  class="k-textbox"/ required></td>
                  </tr>
                  <tr>
                  <td><label> New Password </label> </td>
                  <td><input  type="password" id="new_password" name="new_password" class="k-textbox"/ required></td>
                  </tr>

                 <tr>
                  <td><label> Confirm Password </label> </td>
                  <td><input type="password" id="confm_password" name="confm_password"  class="k-textbox"/ required ></td>
                  </tr>
                  </table>
                </li>
        <li><br>
            <button id="updateTeacherProfile" class="k-button k-primary" type="submit">Update</button>&nbsp;&nbsp;
             <br><br>

    </ul>
</form> 

</div>
@stop

route.php

Route::get('/teacherresetpassword', function () {
return view('layouts.teacher.teacherresetpassword');
 });
 Route::post('/teacherupdatepassword ', 
 'Teacher\TeacherController@TeacherUpdatePassword');

这里会话闪光警报消息已经有效,但我希望验证为空字段,密码不匹配案例。但验证无效,我知道如何制作它请帮助我!

4 个答案:

答案 0 :(得分:0)

你应该尝试这个例子:

public function rules()
    {
        return [
            'old_password' => 'required',
            'password'     => 'required|min:6|confirmed',
        ];
    }

    /*
    *Custom message...
    */
    public function messages(){
        return [
            'old_password.required' => 'The Old Password field is required.',
            'password.required' => 'The New Password field is required.',

        ];
    }

<强>更新

只需将此代码放在confim密码文本框下面

即可
@if (count($errors->first('your_field_name')) > 0)
  <div class="alert alert-danger">
      <ul>
          <li>{{$errors->first('your_field_name')}}</li>
      </ul>
  </div>
@endif

希望这段代码能为你服务!!!

答案 1 :(得分:0)

 $curr_password, $new_password, $confm_password these 3 contains input.
 $data_input=array('curr_password'=>$curr_password, 'new_password'=>$new_password , 'confm_password'=>$confm_password)

 $rules=array(
     'curr_password'   => 'required',
     'new_password'    => 'required|min:4',
     'confm_password'  => 'required|same:new_password');

 $messages=array(
     'curr_password.required'   => 'Enter Current Password',
     'new_password.required'    => 'Enter Password',
     'confm_password.required'  => 'Enter Confiem password');

 Validator::make($data_input, $rules, $messages);

答案 2 :(得分:0)

$this->validate($request,[
    'new_password' => 'required|min:4|confirmed',
    'new_password_confirmation' => 'required|min:4',
], [
    'new_password.required'   => 'The new password field is required',
    'new_password_confirmation.required'    => 'Please repeat the new password',
    'new_password.confirmed'  => 'The new password confirmation does not match',
]);

关于rule-confirmed,希望它有用

答案 3 :(得分:0)

我尝试了自己,最后我得到了上述问题的正确答案。

public function TeacherUpdatePassword(Request $request)
{ 

$curr_password = $request->curr_password;
$new_password  = $request->new_password;
$confm_password  = $request->confm_password;

   $this->validate($request,[
 'curr_password'   => 'required',
 'new_password'    => 'required|min:4',
 'confm_password'  => 'required|same:new_password',
 ]);

 if(!Hash::check($curr_password,Auth::user()->password)){ 
\Session::flash('success', 'The specified password does not match.'); 
return Redirect('/teacherresetpassword');
 } 

  else{
  $request->user()->fill(['password' => Hash::make($new_password)])->save();
 \Session::flash('success', 'Updated Successfully.');   
  return Redirect('/teacherresetpassword');
}

布局

 <label> Current Password </label> 
                   <input type="password" id="curr_password" name="curr_password"  class="k-textbox"/>
                  @if (count($errors->first('curr_password')))
                 <div class="alert alert-danger">
                 <ul>
                <li>{{$errors->first('curr_password')}}</li>
              </ul>
               </div>
                 @endif

                    <label> New Password </label> 
                   <input type="password" id="new_password" name="new_password" class="k-textbox"/>
                  @if (count($errors->first('new_password')))
                 <div class="alert alert-danger">
                 <ul>
                <li>{{$errors->first('new_password')}}</li>
              </ul>
               </div>
                 @endif

                  <label> Confirm Password </label> 
                  <input  type="password" id="confm_password" name="confm_password" class="k-textbox"/>
                  @if (count($errors->first('confm_password')) > 0)
                 <div class="alert alert-danger">
                 <ul>
                <li>{{$errors->first('confm_password')}}</li>
              </ul>
               </div>
                 @endif

感谢所有人给我一个关于我的问题的想法。