验证期间仅显示一次错误消息

时间:2016-04-16 05:27:26

标签: php validation laravel laravel-5.1

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

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

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

$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

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

1 个答案:

答案 0 :(得分:2)

您可以跟踪上一个错误并仅显示错误 如果它与上一个错误不同:

@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