dimanche 7 février 2016

required_without_all validator repeats error message over and over

I'm building a roster system as a side project and on one of the pages you can change the regular hours someone works.

On the page I have a checkbox for each day of the week, you can then go through and select the appropriate days that the person works.

They need to work at least one day and so at least one of the checkboxes needs to be checked when submitted.

To test this I am using the [required_without_all][1] rule of Laravel's validator.

It works perfectly, however if no boxes are checked it will redirect you back and spit out the same error message 7 times (as there are 7 checkboxes for each day of the week).

I am using custom error messages so this is why the error message is the same, but even if I didn't I wouldn't want a similar error message being repeated over and over.

This is what my validator looks like:

$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();
        }

So if no boxes are checked on submission, the error Surely they are working at least one day! is shown 7 times.

I am displaying the errors on the page like this:

@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

Is there anyway to only get it to show once?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire