douxia3505 2018-11-13 07:05
浏览 86
已采纳

使用Laravel进行验证错误消息

I am trying to make validation error message for registration with Laravel. But a bit confused about at end of the form stage. I already made a request, setup the controller connection and the other stuff. All of it already over. But in registration.blade in which part of in that form I use this: @if ($errors->first('name')){{ text-danger}} @endif Every label part? or form part!?

This is my form:

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

Uptade

this is how I did(example):

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

But it's not working.

also this is my requests

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'
        ];
    }

controller

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

1条回答 默认 最新

  • douren5490 2018-11-13 07:13
    关注

    It depends on what you want to achieve:

    Option 1: if you want to show all errors on the top of the form:

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

    Option 2: if you want to show specific error for each input, then you put one block under each input (and replace <!-- Put name validation error messages here -->):

    @if ($errors->first('name'))
        {{ $errors->formName->first('name') }}
    @endif
    
    ....
    
    @if ($errors->first('password'))
        {{ $errors->formName->first('password') }}
    @endif
    
    ....
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?