vendredi 15 avril 2016

Show multiple error messages only once during validation

I have a simple multichoice answer form on one of my pages. All of the fields are required, and I am using validation to check that they are all completed and correct.

When I return my error message, I don't want to say which answer was wrong, I want them to figure this out for themselves. But it is OK to tell them which specific question they haven't filled out.

Given this, I've built my validator like so:

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

The only issue with this is that if one or more of the questions are incorrect, then the same error message, At least one of your answers was incorrect. Please review them and resubmit., is repeated multiple times.

I'm printing it out on the page like so:

@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

Is there an easy way to prevent this from happening or to only print it out once?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire