Reputation: 16339
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
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?
Upvotes: 0
Views: 1067
Reputation: 58
You can use array_unique($array)
function to remove duplicates from error messages.
Here is the code snippet
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach (array_unique($errors->all()) as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
Upvotes: 0
Reputation: 1312
It is late but might help someone with similar problem.
Just apply the rule for a hypothetical field, say, weekdays
like below.
$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!',
]);
And put all the fields (mondayCheckbox.... sundayCheckbox)
that you want this rule to be applied after required_without_all
. And write the validation message for the rule only once.
Upvotes: 0
Reputation: 2658
There are many ways to go around this. Since JS hacks are on the table too, we can also come in from the Blade end and do this (feel free to use your own favourite array manipulation functions):
<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>
The other way is to deal with the Illuminate\Contracts\Support\MessageBag
in your validator with the After Validation Hook and clean things up there.
Upvotes: 2