5

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.

5
  • So you want it to say "Some fields are empty" as in representing multiple fields or return one string of error messages as in "field 1 is empty, field 2 is empty" etc Commented Sep 7, 2014 at 7:28
  • Elaborate on "I want to show only one message for all fields" Commented Sep 7, 2014 at 7:31
  • @Brian Yes. You are right Commented Sep 7, 2014 at 7:42
  • 1
    Ok Im right, but about which of my scenarios :) Commented Sep 7, 2014 at 7:44
  • 1
    @Brian "Some fields are empty" are empty. But I want show this message Once. Commented Sep 7, 2014 at 7:50

1 Answer 1

3

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>') }}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.