【问题标题】:Show multiple error messages only once during validation在验证期间仅显示多条错误消息
【发布时间】:2016-08-08 05:06:09
【问题描述】:

我的一个页面上有一个简单的多项选择答案表。所有字段都是必需的,我正在使用验证来检查它们是否都已完成且正确。

当我返回我的错误消息时,我不想说哪个答案是错误的,我希望他们自己解决这个问题。但可以告诉他们哪些具体问题没有填写。

鉴于此,我已经像这样构建了我的验证器:

$validator = Validator::make($request->all(), [
            'multi1' => 'required|in:b',
            'multi2' => 'required|in:a',
            'multi3' => 'required|in:c',
            'multi4' => 'required|in:b',
            'multi5' => 'required|in:c',
            'multi6' => 'required|in:a',
            'multi7' => 'required|in:c',
            'multi8' => 'required|in:c',
            'multi9' => 'required|in:a',
            'multi10' => 'required|in:b',

        ], [
            'multi1.required' => 'The first question is empty!',
            'multi2.required' => 'The second question is empty!',
            'multi3.required' => 'The third question is empty!',
            'multi4.required' => 'The fourth question is empty!',
            'multi5.required' => 'The fifth question is empty!',
            'multi6.required' => 'The sixth question is empty!',
            'multi7.required' => 'The seventh question is empty!',
            'multi8.required' => 'The eighth question is empty!',
            'multi9.required' => 'The ninth question is empty!',
            'multi10.required' => 'The tenth question is empty!',

            'multi1.in' => 'At least one of your answers was incorrect. Please review them and resubmit.',
            'multi2.in' => 'At least one of your answers was incorrect. Please review them and resubmit.',
            'multi3.in' => 'At least one of your answers was incorrect. Please review them and resubmit.',
            'multi4.in' => 'At least one of your answers was incorrect. Please review them and resubmit.',
            'multi5.in' => 'At least one of your answers was incorrect. Please review them and resubmit.',
            'multi6.in' => 'At least one of your answers was incorrect. Please review them and resubmit.',
            'multi7.in' => 'At least one of your answers was incorrect. Please review them and resubmit.',
            'multi8.in' => 'At least one of your answers was incorrect. Please review them and resubmit.',
            'multi9.in' => 'At least one of your answers was incorrect. Please review them and resubmit.',
            'multi10.in' => 'At least one of your answers was incorrect. Please review them and resubmit.',
        ]);

        if ($validator->fails()) {
            return redirect('/application/multichoice')
                ->withErrors($validator)
                ->withInput();
        }

唯一的问题是,如果一个或多个问题不正确,则相同的错误消息 At least one of your answers was incorrect. Please review them and resubmit. 会重复多次。

我像这样在页面上打印出来:

@if (count($errors) > 0)
    <div class="alert alert-danger">
        <p><b>There are some errors with your answers:</b></p>
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif

有没有一种简单的方法可以防止这种情况发生或只打印一次?

【问题讨论】:

    标签: php validation laravel laravel-5.1


    【解决方案1】:

    您可以跟踪以前的错误并只显示错误 如果和之前的错误不一样:

    @if (count($errors) > 0)
        <div class="alert alert-danger">
            <p><b>There are some errors with your answers:</b></p>
            <ul>
                {{--*/ $prev = null; /*--}}
                @foreach ($errors->all() as $error)
                    @if ($prev != $error)
                    <li>{{ $error }}</li>
                    @endif 
                    {{--*/ $prev = $error; /*--}}
                @endforeach
            </ul>
        </div>
    @endif
    

    【讨论】:

    • 我不得不将 $prev 的声明更改为 {{--*/ $prev = null; /*--}},但除此之外,这一切都很好!
    猜你喜欢
    • 2011-02-03
    • 1970-01-01
    • 1970-01-01
    • 2012-06-04
    • 1970-01-01
    • 2014-11-15
    • 1970-01-01
    • 2020-06-20
    • 1970-01-01
    相关资源
    最近更新 更多