【问题标题】:Laravel 8 - All validation rules are shown if one rule appliesLaravel 8 - 如果一个规则适用,则显示所有验证规则
【发布时间】:2021-11-23 11:25:20
【问题描述】:

我正在使用Laravel Framework 8.62.0,并且我有以下验证规则:

        $rules=[
            'username' => 'required|min:3|max:30|alpha_dash',
            'email' => "required|email|unique:users,email",
            'password' => 'required|min:6|confirmed',
        ];
        $error_messages=[
            'username.required'=>'The username-field is required.',
            'username.min'=>'Your username must be longer than 3 characters.',
            'username.max'=>'Please shorten your username.',
            'username.alpha_dash'=>'Please use letters from A-Z and a-z, dashes (-) or underscores(_).',
            'email.unique'=>'Your email is already used.',
            'email.email'=>'Please add a correct email-address.',
            'email.required'=>'The email-field is required.',
            'password.required'=>'The password-field is required.',
            'password.min'=>'Your password must have at least 6 characters.',
            'password.confirmed'=>'Your entered passwords do not match.',
        ];
        $validator = validator($request->all(), $rules, $error_messages);

我的表单如下所示:

                                                        <form id="Register" class="card-body" tabindex="500" action="{{ url('register') }}" method="POST">
                                <!-- CSRF Token -->
                                <input type="hidden" name="_token" value="{{ csrf_token() }}" />
                                <div class="name">
                                    <input type="text" name="username" value="{!! old('username') !!}">
                                    <label>Username</label>
                                    <li>
                                    {!! $errors->first('username.required', '<ul><span style="color: red;" class="help-block">:message</span></ul>') !!}
                                    {!! $errors->first('username.min', '<ul><span style="color: red;" class="help-block">:message</span></ul>') !!}
                                    {!! $errors->first('username.max', '<ul><span style="color: red;" class="help-block">:message</span></ul>') !!}
                                    {!! $errors->first('username.alpha_dash', '<ul><span style="color: red;" class="help-block">:message</span></ul>') !!}
                                    </li>
                                </div>
                                <div class="mail">
                                    <input type="email" name="email" value="{{ old('email') }}">
                                    <label>Mail</label>
                                    {!!  $errors->first('email.unique', '<span style="color: red;" class="help-block">:message</span>') !!}
                                    {!!  $errors->first('email.required', '<span style="color: red;" class="help-block">:message</span>') !!}
                                    {!!  $errors->first('email.email', '<span style="color: red;" class="help-block">:message</span>') !!}
                                </div>
                                <div class="passwd">
                                    <input type="password" name="password" value="{{ old('password') }}">
                                    <label>Password</label>
                                    {!! $errors->first('password.min', '<span style="color: red;" class="help-block">:message</span>') !!}
                                    {!! $errors->first('password.confirmed', '<span style="color: red;" class="help-block">:message</span>') !!}
                                    {!! $errors->first('password.required', '<span style="color: red;" class="help-block">:message</span>') !!}
                                </div>
                                <div class="passwd">
                                    <input type="password" name="password_confirmation" value="{{ old('password_confirmation') }}">
                                    <label>Confirm Password</label>
                                    {!! $errors->first('password.confirmed', '<span style="color: red;" class="help-block">:message</span>') !!}
                                </div>
                                <div class="submit">
                                    <!-- <a class="btn ripple  btn-primary btn-block" href="#">Register</a> -->
                                    <input type="submit" class="btn ripple  btn-primary btn-block" value="Register">
                                </div>
                                <p class="text-dark mb-0">Already have an account?<a href="{{ url('login') }}" class="text-primary ml-1">Sign In</a></p>
                            </form>

当我输入一个包含 3 个字符且没有 - 的用户名并且我的密码长度不匹配时,我会返回所有验证错误:

但是,我只想找回真正适用的错误。

任何建议我做错了什么?

感谢您的回复!

【问题讨论】:

    标签: laravel validation


    【解决方案1】:

    您要求刀片文件按名称/键显示您创建的每个错误,而不是查看错误包中的内容。

    Laravel 会自动显示适用于您页面的错误,并将输出限制为仅适用的错误。您不需要像 $errors-&gt;first('email.unique')... 等那样按名称单独捕获它们。

    看看the docs on how to display validation errors,它非常漂亮。基本上检查错误包,如果出现,只显示包中的那些:

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

    $errors 变量可在 Web 中间件下的路由内的任何视图中使用。

    【讨论】:

      猜你喜欢
      • 2022-01-09
      • 1970-01-01
      • 2015-02-27
      • 2016-05-13
      • 2016-12-17
      • 2020-06-25
      • 2020-06-09
      • 2021-12-09
      • 1970-01-01
      相关资源
      最近更新 更多