0

I have a form in which a user can add as many contacts as he'd like. But I get stuck on the validation for this. I use the old way of validating forms, using the $request->validate method.

My form (part of it that is relevant):

<div class="row">
    <div class="col">
        {!! Form::text('contact_name[]', __('site.contact.name'))->placeholder(__('site.contact.name'))->help('<a href="#add-contact" id="add-contact">+ ' . __('site.contact.add_person') . '</a>') !!}
    </div>
    <div class="col">
        {!! Form::text('contact_email[]', __('site.contact.email'))->placeholder(__('site.contact.email')) !!}
    </div>
    <div class="col">
        {!! Form::text('contact_phone[]', __('site.contact.phone'))->placeholder(__('site.contact.phone')) !!}
    </div>
</div>

And my validation is in the CRUD store method:

$request->validate([
    'contact_name.*' => 'required|max:255',
    'contact_email.*' => 'required|email|max:255',
    'contact_phone.*' => 'required|max:255',
]);

This always results in a pass, while there is nothing in the input. When I try the following; it always fails.

$request->validate([
    'contact_name[]' => 'required|max:255',
    'contact_email[]' => 'required|email|max:255',
    'contact_phone[]' => 'required|max:255',
]);

According to the documentation, the wildcard way, is the way it should be. Any ideas on how to get this working?

Kind regards, Niels

-- EDIT 1 --

My form request data looks like this (only relevant variables included)

"_token" => "IbYI2FYivl30YJfw8hBHti9bsaBUdkG2T76HZOIN"
"contact_name" => array:1 [▼
    0 => "abc"
]
"contact_email" => array:1 [▼
    0 => "abc"
]
"contact_phone" => array:1 [▼
    0 => "abc"
]
nepp95
  • 131
  • 1
  • 13

2 Answers2

0

Change your validation like this one

$request->validate([
    'contact_name'   => "required|array",
    'contact_name.*' => 'required|max:255',
    'contact_email'   => "required|array",
    'contact_email.*' => 'required|email|max:255',
    'contact_phone'   => "required|array",
    'contact_phone.*' => 'required|max:255',
]);

In the validation above:

"contact_name" must be an array. Values in the "contact_name" array must be required, at least 255 characters long.

Or

$request->validate([
    '*.contact_name' => 'required|max:255',
    '*.contact_email' => 'required|email|max:255',
    '*.contact_phone' => 'required|max:255',
]);
Qonvex620
  • 3,819
  • 1
  • 8
  • 15
  • Unfortunately this doesn't work. To clarify, I have added the request data to the question. It isn't logical to me because `contact_name.*` would check `contact_name[0]` and all following indexes... But it doesn't. – nepp95 Jan 20 '20 at 13:34
0

(*) checks values in the array, not the actual array. If you add wildcard rules you'll get the parent key as validated always. So, you also have to add an array check for each field:

'field' => 'array|required'

Maharramoff
  • 941
  • 8
  • 15
  • Does not work unfortunately. The data is always an array. Even with 1 input posted. Check first post, I have added the request data. – nepp95 Jan 20 '20 at 13:47