Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set One Custom message for multiple field in laravel form validation

Let I have 3 fields in the form.

I write this code for form validation.

$rules = array(
                'field_1' => 'required',
                'field_2' => 'required',
                'field_3' => 'required',
            );


$messages = array(
                'field_1.required' => 'Please fill up all value',
                'field_2.required' => 'Please fill up all value',
                'field_3.required' => 'Please fill up all value',
            );

$validator = Validator::make(Input::all(), $rules, $messages);

Now if user fill up the form 2 fields with blank then the validator return two error message. If user fill up the form 3 fields with blank then the validator return three error message. But I want to show only one message for all fields.

like image 287
Nur Uddin Avatar asked Oct 20 '22 02:10

Nur Uddin


1 Answers

you could change it to

$messages = array(
    'required' => 'Please fill up all value',
);
validator = Validator::make(Input::all(), $rules, $messages);

and let your view only output the first error message.

{{ $errors->first(null,'<div class="alert alert-danger">:message</div>') }}
like image 154
Pᴇʜ Avatar answered Oct 23 '22 00:10

Pᴇʜ