簡體   English   中英

Laravel 5使用After Validation Hook在數組中添加錯誤

[英]Laravel 5 Add errors in an array using 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掛鈎 我不知oldpassword什么問題,但是當我輸入錯誤的密碼時,它似乎無法驗證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
}

有什么想法嗎?

根據您的代碼,當您輸入正確的舊密碼時,您會收到錯誤消息。 因此,將if(Auth::attempt.....更改為if(!Auth:attempt...此外,如果您使用Auth:attempt,則必須再次注銷用戶(此方法還需要唯一的字段,例如用戶名或電子郵件地址,識別用戶)。因此,最好使用以下方法

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM