2016-07-31 2 views
0

사용자가 계정 암호를 변경할 수있는 기능을 응용 프로그램에 추가하려고합니다. 나는 세 개의 필드를 가지고 내보기는 다음과 같습니다Laravel 5 After Validation Hook을 사용하여 배열에 오류 추가

<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...으로 변경하십시오. 또한 Auth : 사용자를 다시 로그 아웃해야합니다 (이 방법은 사용자를 식별하기 위해 사용자 이름이나 전자 메일과 같은 고유 한 필드가 필요합니다). 당신이 방법

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

감사합니다. :) .. –