0

I'm having trouble with Laravel 5.1 to validate an array.

HTML CODE:

{!! Form::label('information[1][color]', 'Color') !!}
{!! Form::select('information[1][color]', [null => 'Choose your color'] + $product->availableColors()->toArray() , null, ['id' => 'select-color0', 'data-id' => $product->id, 'class' => 'form-control', 'required' => 'required']) !!}

To test if the data is being passed on correctly:

dd($request->all):

"information" => array:1 [▼
    1 => array:1 [▼
      "color" => "2"
    ]
  ]

Validation code:

public function store(Request $request)
    {
        $rules = [
            'information.*.color' => 'required'
        ];

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

        if($validator->fails()){
            dd('Validation fails');
            return Redirect::to('index')->withErrors($validator);
        } else {
            ..........

I keep ending up at the page from the dd('Validation fails'). Can someone explain to me what I'm missing here and why the validation isn't working? I'm trying to follow this tutorial: https://mattstauffer.com/blog/form-array-validation-in-laravel-5-2/

I have also tried:

$validator = Validator::make($request->all(), [
    'information' => 'required',
    'information.*.color' => 'required'
]);

but the validation still fails

Also tried changing the form name:

{!! Form::select('information[][color]', [null => 'Choose your color'] + $product->availableColors()->toArray() , null, ['id' => 'select-color0', 'data-id' => $product->id, 'class' => 'form-control', 'required' => 'required']) !!}

But still no luck yet

2
  • could you please add output also? Commented Aug 17, 2018 at 11:16
  • instead of Input you should try $request. Commented Aug 17, 2018 at 11:21

2 Answers 2

1

SOLUTION:

$rules = [
        'id' => 'required',
        'name' => 'required|string',
        'information' => 'required|array'
    ];

    foreach ($request->information as $key => $variant) {
        foreach($variant as $color){
            $rules['information.'.$key.'.color'] = 'required';
        }
    }

    $validator = Validator::make($request->all(), $rules);
Sign up to request clarification or add additional context in comments.

1 Comment

I still think the 'information' => 'required|array' is conflicting with the elegant way, and that may not be needed... but anyways, good that you solved it!
0

Remove the 1 from the select name

{!! Form::select('information[][color]', array_merge([null => 'Choose your color'],$product->availableColors()->toArray()) , null, ['id' => 'select-color0', 'data-id' => $product->id, 'class' => 'form-control', 'required' => 'required']) !!}

or change your roules to

$rules = [
            'information.1.color' => 'required'
        ];

8 Comments

This is also not the problem because as you can see from the dd($request->all()) the values are passed on correctly. Somehow the validation isn't working on the array.
now that update, which is a different point, actually is closer to what the issue is
@lagbox maybe you can help answering the question instead of just saying what is wrong!
@madalinivascu thank you for the updated suggestion. I think this is indeed closer to solving the problem. The form is dynamic. So a user can add another color input form. I'm currently working on just getting the validation for a single color to work.
@AnnaJeanine are you submitting the form via ajax or via a input form?do you have any other inputs on the form?
|

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.