required_without_all验证器一遍又一遍地重复错误消息

时间:2016-02-08 03:48:08

标签: php validation laravel laravel-5.1

我建立一个名册系统作为辅助项目,在其中一个页面上你可以改变有人工作的正常工作时间。

在页面上,我有一个每周的每一天的复选框,然后您可以选择该人工作的适当日期。

他们需要至少工作一天,因此在提交时至少需要检查一个复选框。

为了测试这个,我使用的是Laravel验证器的required_without_all规则。

它完美无缺,但是如果没有选中框,它会重定向你并且会发出相同的错误消息7次(因为每周的每一天有7个复选框)。

我正在使用自定义错误消息,所以这就是为什么错误消息是相同的,但即使我没有想要一遍又一遍地重复类似的错误消息。

这是我的验证器的样子:

$validator = Validator::make($request->all(), [
            'mondayCheckbox' => 'required_without_all:tuesdayCheckbox,wednesdayCheckbox,thursdayCheckbox,fridayCheckbox,saturdayCheckbox,sundayCheckbox',
            'tuesdayCheckbox' => 'required_without_all:mondayCheckbox,wednesdayCheckbox,thursdayCheckbox,fridayCheckbox,saturdayCheckbox,sundayCheckbox',
            'wednesdayCheckbox' => 'required_without_all:mondayCheckbox,tuesdayCheckbox,thursdayCheckbox,fridayCheckbox,saturdayCheckbox,sundayCheckbox',
            'thursdayCheckbox' => 'required_without_all:mondayCheckbox,tuesdayCheckbox,wednesdayCheckbox,fridayCheckbox,saturdayCheckbox,sundayCheckbox',
            'fridayCheckbox' => 'required_without_all:mondayCheckbox,tuesdayCheckbox,wednesdayCheckbox,thursdayCheckbox,saturdayCheckbox,sundayCheckbox',
            'saturdayCheckbox' => 'required_without_all:mondayCheckbox,tuesdayCheckbox,wednesdayCheckbox,thursdayCheckbox,fridayCheckbox,sundayCheckbox',
            'sundayCheckbox' => 'required_without_all:mondayCheckbox,tuesdayCheckbox,wednesdayCheckbox,thursdayCheckbox,fridayCheckbox,saturdayCheckbox',
            'effective_from' => 'date',
            ], [
            'mondayCheckbox.required_without_all' => 'Surely they are working at least one day!',
            'tuesdayCheckbox.required_without_all' => 'Surely they are working at least one day!',
            'wednesdayCheckbox.required_without_all' => 'Surely they are working at least one day!',
            'thursdayCheckbox.required_without_all' => 'Surely they are working at least one day!',
            'fridayCheckbox.required_without_all' => 'Surely they are working at least one day!',
            'saturdayCheckbox.required_without_all' => 'Surely they are working at least one day!',
            'sundayCheckbox.required_without_all' => 'Surely they are working at least one day!',
            'effective_from.date' => 'You have provided an invalid date for when their hours are effective from!',
            ]);

        if ($validator->fails())
        {
            return Redirect::back()
                ->withErrors($validator)
                ->withInput();
        }

因此,如果在提交时未选中任何框,则会显示错误Surely they are working at least one day! 7次。

我在页面上显示的错误如下:

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

无论如何只能让它显示一次吗?

2 个答案:

答案 0 :(得分:1)

有很多方法可以解决这个问题。由于JS hacks也在桌面上,我们也可以从Blade端进入并执行此操作(随意使用您自己喜欢的数组操作函数):

<ul>

    <li>
        {{-- Show error message only once --}}
        @if( $errors->has('mondayCheckbox') || $errors->has('tuesdayCheckbox') ||  $errors->has('wednesdayCheckbox') ||  $errors->has('thursdayCheckbox') ||  $errors->has('fridayCheckbox') ||  $errors->has('saturdayCheckbox') ||  $errors->has('sundayCheckbox') )
            Surely they are working at least one day!
        @endif
    </li>

    @foreach ($errors->all() as $error)

        {{-- Show other errors not related to the checkboxes --}}
        @unless($error == 'Surely they are working at least one day!')
            <li>
                {{ $error }}
            </li>
        @endunless

    @endforeach
</ul>

另一种方法是使用After Validation Hook处理验证器中的Illuminate\Contracts\Support\MessageBag并清理其中的内容。

答案 1 :(得分:0)

很晚了,但可能会帮助遇到类似问题的人。 只需将规则应用于假设字段,例如 weekdays,如下所示。

$validator = Validator::make($request->all(), [
            'weekdays' => 'required_without_all:mondayCheckbox,tuesdayCheckbox,wednesdayCheckbox,thursdayCheckbox,fridayCheckbox,saturdayCheckbox,sundayCheckbox',
            'effective_from' => 'date',
            ], [
            'required_without_all' => 'Surely they are working at least one day!',
            'effective_from.date' => 'You have provided an invalid date for when their hours are effective from!',
            ]);

并将您希望应用此规则的所有字段 (mondayCheckbox.... sundayCheckbox) 放在 required_without_all 之后。并且只为规则编写一次验证消息。