簡體   English   中英

required_without_all 驗證器一遍又一遍地重復錯誤消息

[英]required_without_all validator repeats error message over and over

我正在建立一個花名冊系統作為一個副項目,在其中一個頁面上,您可以更改某人的正常工作時間。

在頁面上,我有一個一周中每一天的復選框,然后您可以通過 go 和 select 該人工作的適當日期。

他們至少需要工作一天,因此提交時至少需要勾選其中一個復選框。

為了測試這一點,我使用了 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

無論如何只能讓它顯示一次?

有很多方法可以解決這個問題。 由於 JS hack 也在討論中,我們也可以從 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並清理那里的內容。

為時已晚,但可能會幫助遇到類似問題的人。 只需將規則應用於假設字段,例如,如下所示的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 並且只為規則編寫一次驗證消息。

您可以使用array_unique($array) function 從錯誤消息中刪除重復項。

這是代碼片段

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM