Laravel 5确认错误消息显示在错误的字段中

时间:2015-06-20 12:48:36

标签: laravel laravel-5 laravel-validation

我想检查一下我的代码中是否有错误我没有意识到,但为什么我的确认字段的错误信息会继续显示在错误的字段中?假设我有一个密码字段和一个password_confirmation字段,但每次用户在password_confirmation字段中输入错误时,错误都会显示在密码字段中。以下是错误的屏幕截图。

invalid error message display

以下是我使用FormRequest验证输入的方法:

public function rules()
{
    $rules = [
    'login_email'           =>  'required',
    'g-recaptcha-response'  =>  'required|captcha',
    ];

    if ($this->request->has('password'))
    {
        $rules = [
        'password' => 'required|confirmed|between:6,200',
        'password_confirmation' => 'required',
        'g-recaptcha-response'  =>  'required|captcha',
        ];
    }

    if ($this->request->has('old_password'))
    {
        $rules = [
        'old_password' => 'required|between:6,200',
        'password' => 'required|confirmed|between:6,200',
        'password_confirmation' => 'required',
        'g-recaptcha-response'  =>  'required|captcha',
        ];
    }

    return $rules;
}

最后这是我的HTML代码

{!! Form::open(array('class' => 'form-horizontal login-form', 'url' => URL::route('postReset'), 'method' => 'POST')) !!}
            <div class="form-group">
                <div class="col-md-12 col-sm-12">
                    {!! Form::password('password', array('class' => 'form-control', 'placeholder' => 'New Password*')); !!}
                    {!! MessagerService::setInlineError($errors->first('password')) !!}
                </div>
            </div>

            <div class="form-group">
                <div class="col-md-12 col-sm-12">
                    {!! Form::password('password_confirmation', array('class' => 'form-control', 'placeholder' => 'Retype New Password*')); !!}
                    {!! MessagerService::setInlineError($errors->first('password_confirmation')) !!}
                </div>
            </div>

            <div class="form-group">
                <div class="col-md-12 col-sm-12">
                    {!! app('captcha')->display(); !!}
                    {!! MessagerService::setInlineError($errors->first('g-recaptcha-response')) !!}
                </div>
            </div>                  

            <div class="form-group">
                <div class="col-md-12 col-sm-12">
                    <button type="submit" class="btn btn-primary cbtn-login">Reset Password</button>
                </div>
            </div>

            <input type="hidden" name="_token" value="{{ csrf_token() }}">
            <input type="hidden" name="hash" value="{{ Session::get('hash') }}">
{!! Form::close() !!}

1 个答案:

答案 0 :(得分:1)

因为您已为密码字段定义了已确认规则,所有与password_confirmation相关的错误也会显示在密码字段中。