0
votes

I post my input in an array like this:

<input class="form-control" id="first_name" name="main_user[first_name]" type="text">

And I want to use the Laravel Requests to validate this input. Usually you determine the validation rules like this in a Request file:

return ['first_name' => 'required']

But this won't work because first_name is posted inside an array. I tried this, but it doesn't work.

return ['main_user[first_name]' => 'required']
2
Use Form Requests and create dynamic rules for your arrays - Rotimi

2 Answers

0
votes

Use * character when validating array. An example:

'person.*.email' => 'email|unique:users',
'person.*.first_name' => 'required_with:person.*.last_name',

https://laravel.com/docs/5.4/validation#validating-arrays

0
votes

I was just thinking to hard with the []. I should have used dots like this:

return [
    'main_user.first_name' => 'required',
]

and

return [
    'main_user.first_name.required' => 'Je moet een voornaam invullen 
     voor het beheerders account.',
]