繁体   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