使用Laravel进行验证错误消息

时间:2018-11-13 07:05:05

标签: php laravel laravel-5

我正在尝试验证错误消息以向Laravel注册。但是在表单阶段结束时有些困惑。我已经提出了一个请求,设置了控制器连接和其他内容。全部都结束了。但是在registration.blade中,我使用该表格的哪一部分:@if ($errors->first('name')){{ text-danger}} @endif每个标签部分?或组成一部分!?

这是我的表格:

<form class="form-horizontal" role="form" method="post" action="{{url('/auth/register')}}">
                {{csrf_field()}}
                <div class="row">
                    <div class="col-md-3"></div>
                    <div class="col-md-6">
                        <h2>新ユーザ</h2>
                        <hr>
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-3 field-label-responsive">
                        <label for="name">名前</label>
                    </div>
                    <div class="col-md-6">
                        <div class="form-group">
                            <div class="input-group mb-2 mr-sm-2 mb-sm-0">
                                <div class="input-group-addon" style="width: 2.6rem"><i class="fa fa-user"></i></div>
                                <input type="text" name="name" class="form-control" id="name"
                                       placeholder="田中 松本" required autofocus>
                            </div>
                        </div>
                    </div>
                    <div class="col-md-3">
                        <div class="form-control-feedback">
                        <span class="text-danger align-middle">
                            <!-- Put name validation error messages here -->
                        </span>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-3 field-label-responsive">
                        <label for="email">E-Mail Address</label>
                    </div>
                    <div class="col-md-6">
                        <div class="form-group">
                            <div class="input-group mb-2 mr-sm-2 mb-sm-0">
                                <div class="input-group-addon" style="width: 2.6rem"><i class="fa fa-at"></i></div>
                                <input type="text" name="email" class="form-control" id="email"
                                       placeholder="you@example.com" required autofocus>
                            </div>
                        </div>
                    </div>
                    <div class="col-md-3">
                        <div class="form-control-feedback">
                        <span class="text-danger align-middle">
                            <!-- Put e-mail validation error messages here -->
                        </span>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-3 field-label-responsive">
                        <label for="password">Password</label>
                    </div>
                    <div class="col-md-6">
                        <div class="form-group has-danger">
                            <div class="input-group mb-2 mr-sm-2 mb-sm-0">
                                <div class="input-group-addon" style="width: 2.6rem"><i class="fa fa-key"></i></div>
                                <input type="password" name="password" class="form-control" id="password"
                                       placeholder="Password" required>
                            </div>
                        </div>
                    </div>
                    <div class="col-md-3">
                        <div class="form-control-feedback">
                        <span class="text-danger align-middle">
                            <i class="fa fa-close"> Example Error Message</i>
                        </span>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-3 field-label-responsive">
                        <label for="password_confirmation">Confirm Password</label>
                    </div>
                    <div class="col-md-6">
                        <div class="form-group">
                            <div class="input-group mb-2 mr-sm-2 mb-sm-0">
                                <div class="input-group-addon" style="width: 2.6rem">
                                    <i class="fa fa-repeat"></i>
                                </div>
                                <input type="password" name="password_confirmation" class="form-control"
                                       id="password-confirm" placeholder="Password" required>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-3"></div>
                    <div class="col-md-6">
                        <button type="submit" class="btn btn-success"><i class="fa fa-user-plus"></i> Register</button>
                    </div>
                </div>
            </form>

更新

这就是我的做法(示例):

 <div class="col-md-3">
    <div class="form-control-feedback @if ($errors->first('name')){{'text-danger'}} @endif ">
          <span class="text-danger align-middle">
            {{$errors->first('')}}
          </span>
     </div>
 </div>

但是它不起作用。

这也是我的请求

public function rules()
    {
        return [
            'name' => 'required|min:3|max:20',
            'email' => 'required|email|unique:users,email',
            'password' => 'required|min:6|max:20|confirmed',
            'password_conformation' => 'required|min:6|max:20'
        ];
    }

控制器

public function register(registerRequest $request)
    {
        return view('auth.register')->withErrors($request);
    }

1 个答案:

答案 0 :(得分:0)

这取决于您要实现的目标:

选项1:如果要在表单顶部显示所有错误:

@if($errors->has())
   @foreach ($errors->all() as $error)
      <div>{{ $error }}</div>
  @endforeach
@endif

选项2:如果要显示每个输入的特定错误,则在每个输入下放置一个块(并替换<!-- Put name validation error messages here -->):

@if ($errors->first('name'))
    {{ $errors->formName->first('name') }}
@endif

....

@if ($errors->first('password'))
    {{ $errors->formName->first('password') }}
@endif

....