【问题标题】:Laravel 5 confirmation error message display in wrong fieldLaravel 5确认错误消息显示在错误字段中
【发布时间】:2015-06-21 01:44:23
【问题描述】:

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

这是我使用 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() !!}

【问题讨论】:

  • 你从哪里得到这门课? MessagerService

标签: laravel laravel-5 laravel-validation


【解决方案1】:

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

【讨论】:

  • 这种情况我该怎么办?我无法将已确认的规则更改为 password_confirmation 字段,否则将无法验证。
  • 您应该从密码错误列表中取出“密码确认不匹配”并显示在password_confirmation字段中。
猜你喜欢
  • 2015-09-12
  • 2013-08-03
  • 2017-01-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-25
  • 2016-06-14
  • 2016-09-29
相关资源
最近更新 更多