在Laravel 5.5注册表单的错误中显示问题

时间:2018-11-13 17:16:08

标签: php laravel laravel-5.5

我在Laravel 5.5中使用make:auth命令创建了一个注册表。它的工作正常,但在验证失败时不显示错误。我已经多次检查模板中是否存在显示错误的代码,但仍然收到此错误。

模板位置:

resources/views/mybladetemplate

控制器中的方法:

protected function validator(array $data)
{
    return Validator::make($data, [
        'field1' => 'required|string|max:255',
        'emailField' => 'required|string|email|max:255|unique:users',
        'fieldPwd' => 'required|string|min:6|confirmed',
        'anotherfield' => 'string'
    ]);
}

RegistersUsers特性:

public function register(Request $request)
{
    $this->validator($request->all())->validate();

    event(new Registered($user = $this->create($request->all())));

    $this->guard()->login($user);

    return $this->registered($request, $user)
                    ?: redirect($this->redirectPath());
}

注册表格:

<form class="form-horizontal" method="POST" action="{{ route('register') }}">
                    {{ csrf_field() }}

                    <div class="form-group{{ $errors->has('field1') ? ' has-error' : '' }}">
                        <label for="name" class="col-md-4 control-label">Field Name</label>

                        <div class="col-md-6">
                            <input id="field1" type="text" class="form-control" name="field" value="{{ old('field1') }}" required autofocus>
                            @if ($errors->has('field1'))
                                <span class="help-block">
                                    <strong>{{ $errors->first('field1') }}</strong>
                                </span>
                            @endif
                        </div>
                    </div>

                    <div class="form-group{{ $errors->has('emailField') ? ' has-error' : '' }}">
                        <label for="email" class="col-md-4 control-label">E-Mail Address</label>

                        <div class="col-md-6">
                            <input id="emailField" type="email" class="form-control" name="emailField" value="{{ old('emailField') }}" required>

                            @if ($errors->has('emailField'))
                                <span class="help-block">
                                    <strong>{{ $errors->first('emailField') }}</strong>
                                </span>
                            @endif
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="anotherfield" class="col-md-4 control-label">Another Field</label>

                        <div class="col-md-6">
                            <input id="anotherfield" type="text" class="form-control" name="anotherfield" value="{{ old('anotherfield') }}" />
                        </div>
                    </div>

                    <div class="form-group{{ $errors->has('fieldPwd') ? ' has-error' : '' }}">
                        <label for="fieldPwd" class="col-md-4 control-label">Password</label>

                        <div class="col-md-6">
                            <input id="fieldPwd" type="password" class="form-control" name="fieldPwd" required>

                            @if ($errors->has('fieldPwd'))
                                <span class="help-block">
                                    <strong>{{ $errors->first('fieldPwd') }}</strong>
                                </span>
                            @endif
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="password-confirm" class="col-md-4 control-label">Confirm Password</label>

                        <div class="col-md-6">
                            <input id="password-confirm" type="password" class="form-control" name="password_confirmation" required>
                        </div>
                    </div>

                    <div class="form-group">
                        <div class="col-md-6 col-md-offset-4">
                            <button type="submit" class="btn btn-primary">
                                Register
                            </button>
                        </div>
                    </div>
                </form>

1 个答案:

答案 0 :(得分:0)

在刀片中使用此简单代码以ul li结构显示所有错误:

        @if($errors->any())
            @foreach ($errors->all() as $error)
                <div class="alert alert-warning" dir="rtl">
                    <ul>
                        <li>{!! $error !!}</li>
                    </ul>
                </div>
            @endforeach
        @endif