5

I am submitting an array of inputs to my controller like so:

<input id="box-1-nickname" name="box-nickname[]" class="form-control" type="text" placeholder="Required">
<input id="box-2-nickname" name="box-nickname[]" class="form-control" type="text" placeholder="Required">

I am doing some validation like this:

$validator = Validator::make(Input::all(), array(
        'supplies-count' => 'required|in:0,1,2,3,4',
    ));

$arrayValidator = Validator::make(Input::all(), []);

$arrayValidator->each('box-nickname', ['required|min:1|max:60']);

if( $validator->fails() || $arrayValidator->fails() ) {
    return Redirect::route('route-2')
           ->withErrors($arrayValidator)
           ->withInput();
}

The problem is when I try to check the errors like this it doesn't work:

if( $errors->has('box-1-nickname') ) { echo ' has-error'; }
4
  • 1
    what happens when you dd($errors->all())? Also, what is the point of your $arrayValidator? Commented Feb 5, 2015 at 4:39
  • @Victor if I add two boxes with no box-nickname and submit I get this from dd($errors->all()) laravel.io/bin/vBjXv Commented Feb 5, 2015 at 18:40
  • Looks like you want to add a class, but it will be difficult since you are passing an array as the input name. You may have to rename your inputs. Commented Feb 5, 2015 at 18:44
  • @DavidNguyen correct I am passing an array because box inputs can be added and removed dynamically on the front end so I don't know how many might be submitted. Commented Feb 5, 2015 at 19:04

4 Answers 4

15

Displaying input array errors in the view (L5.8 onwards)

To get the first validation error for an input array:

{{ $errors->first('input_array.*') }}

To check if there is an error within an input array:

@if($errors->has('input_array.*'))
    <h1>There is an error in your input array</h1>
    <ul>
       @foreach($errors->get('input_array.*') as $errors)
           @foreach($errors as $error)
               <li>{{ $error }}</li>
           @endforeach
       @endforeach
    </ul>
@endif

Other examples:

@error('input_array.*')
    <div class="alert alert-danger">{{ $message }}</div>
@enderror

From 5.8^ documentation

Working with error messages

If you are validating an array form field, you may retrieve all of the messages for each of the array elements using the * character:

foreach ($errors->get('attachments.*') as $message) {
    //
}

Hope it helps!

Sign up to request clarification or add additional context in comments.

Comments

3

You've probably long found a solution, but for anyone else who stumbles across this:

The validator uses array dot notation of the field array keys. For example box-nickname[0] becomes box-nickname.0

Therefore if( $messages->has('box-nickname.0') ) { echo ' has-error'; } should give you your desired result. However, you will need to dynamically generate the array key since as you've said, you won't know how many box-nicknames are being applied. I use this in my form view:

@if(!is_null(Input::old('box-nickname')))
    @foreach(Input::old('box-nickname') as $n => $box-nickname)
        @include('box-nickname-create-form-partial')
    @endforeach
@endif

Then create a partial view called "box-nickname-create-form-partial.blade.php" or whatever you want to call it with the form field, which might look something like this:

<div class="form-group {!! $errors->has('box-nickname.'.$n) ? ' has-error' : '' !!}">
    <input name="box-nickname[{{$n}}]" class="form-control" type="text" placeholder="Required">
</div>

I hope that's helpful.

Comments

0

The errors are collected by name property, not id, and Laravel's default MessageBag variable is $messages, not $errors:

if( $messages->has('box-nickname') ) { echo ' has-error'; }

http://laravel.com/docs/4.2/validation#working-with-error-messages

4 Comments

Could be 'box-nickname[]' instead, I don't know off the top of my head.
I tried box box-nickname and box-nickname[] but no luck.
@JoshMountain This just occurred to me - try $messages instead of $errors: laravel.com/docs/4.2/validation#working-with-error-messages
$messages is undefined
0

$errors is correct, but you should be checking for box-nickname. As you can see you will run into the issue of not being able to identify what box is what because of the generic name. I think the easiest way to to give each input a unique name (eg. box-1, box-2)and do a for loop on the server side to retrieve inputs that start with box-.

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.