2016-07-31 4 views
0

ユーザーが自分のアカウントパスワードを変更できる機能をアプリケーションに追加しようとしています。私は3つのフィールドがあり、私の見解は次のようになります。まずLaravel 5 After Validationフックを使用して配列にエラーを追加する

<form class="form" role="form" action="{{ url('users/updatePassword') }}" method="post"> 
    {{ csrf_field() }} 

    <div class="form-group label-floating {{ $errors->has('oldpassword') ? 'has-error' : '' }}"> 
     <label class="control-label" for="oldpassword">Old Password</label> 
     <input type="password" name="oldpassword" class="form-control"> 

     @if ($errors->has('oldpassword')) 
      <span class="help-block"> 
       <strong>{{ $errors->first('oldpassword') }}</strong> 
      </span> 
     @endif 
    </div> 

    <div class="form-group label-floating {{ $errors->has('newpassword') ? 'has-error' : '' }}"> 
     <label class="control-label" for="newpassword">New Password</label> 
     <input type="password" name="newpassword" class="form-control"> 

     @if ($errors->has('newpassword')) 
      <span class="help-block"> 
       <strong>{{ $errors->first('newpassword') }}</strong> 
      </span> 
     @endif 
    </div> 

    <div class="form-group label-floating"> 
     <label class="control-label" for="newpassword_confirmation">Confirm Password</label> 
     <input type="password" name="newpassword_confirmation" class="form-control"> 
    </div> 

    <div class="form-group"> 
     <button class="btn btn-raised btn-primary">Change</button> 
    </div> 
</form> 

、私はすべてのフィールドが完全に満たされ、そのために私はValidatorを使用しているかどうかを確認します。そしてoldpasswordがデータベースと一致するので、if (Auth::attempt(array('password' => $request->oldpassword)))の条件を使用しているかどうかを確認してください。私はlaravel 5.2のドキュメントでAfter Validation hookも見つけました。何が間違っているのかわかりませんが、間違ったパスワードを入力したときにoldpasswordフィールドを検証しないようです。

マイコントローラ:

$validator = Validator::make($request->all(), [ 
    'oldpassword' => 'required|max:255', 
    'newpassword' => 'required|min:6|max:255|confirmed', 
    ]); 
$validator->after(function($validator) use($request) { 
    if (Auth::attempt(array('password' => $request->oldpassword))) { 
     $validator->errors()->add('oldpassword', 'Old password dont match in our database.'); 
    } 
}); 
if ($validator->fails()) { 
    // Toastr 
    $title = "Oops!"; 
    $message = "Please make sure to fill all required fields."; 
    $options = [ 
     'progressBar' => false, 
     'positionClass' => 'toast-top-right', 
     'timeOut' => 6000, 
    ]; 
    Toastr::error($message, $title, $options); 
    return redirect()->back() 
     ->withErrors($validator); 
} else { 
    return 'success'; // for testing only 
} 

これに関するすべてのアイデア?

答えて

1

あなたのコードによれば、正しいoldpasswordを入力すると、エラーが発生します。したがってif(Auth::attempt.....からif(!Auth:attempt...に変更してください。また、認証を使用する場合:ユーザーを再度ログアウトする必要があります(この方法でも、ユーザーを識別するためのユーザー名や電子メールなどの固有のフィールドが必要です)。あなたがこの方法

if (!\Hash::check($request->get('oldpassword'), \Auth::user()->password)) { 
    $validator->errors()->add('oldpassword', 'Old password dont match in our database.'); 
} 
+0

Arrg以下の使用している場合ので、それは良いでしょう! "!"今働いている。ドキュメンテーション内で 'Hash :: check()'の詳細を読むことができるリンクはありますか? –

+1

はい、https://laravel.com/docs/5.2/hashing –

+0

ありがとうございます! :) .. –

関連する問題