0

I'm trying to validate some input array fields in Laravel. My application reports back the field is required, even though it has been filled out.

The validation code I have is:

$this->validate($request, [
        'first_name'        => 'required',
        'last_name'         => 'required',
        'telephone'         => 'required',
        'email'             => 'unique:contacts,email,' . $request->id,
        'address'           => 'required',
        'address.0.address_line_1'         => 'required',
    ]);

The full posted array is:

Array
(
[id] => 1
[_token] => xxx
[first_name] => Joe
[last_name] => Bloggs
[contact_name] => Joe Bloggs
[telephone] => 077
[email] => [email protected]
[address] => Array
    (
        [0] => Array
            (
                ['address_line_1'] => sss
                ['address_line_2'] => 
                ['city'] => 
                ['county'] => 
                ['postcode'] => 
                ['property_type'] => site
            )

    )

)

My input fields are constructed like so:

address[0]['address_line_1']

I'm getting the validation message error:

The address.0.address line 1 field is required.

Anyone know what's wrong here?

4
  • duplicate : stackoverflow.com/questions/42258185/… Commented Mar 5, 2022 at 17:06
  • It's not a duplicate - i've tried what they said in the same thread, but it doesn't work for me. Commented Mar 5, 2022 at 17:14
  • What if you remove your requirement of address? It might conflict. Commented Mar 5, 2022 at 19:54
  • You mean change the name Commented Mar 5, 2022 at 19:59

3 Answers 3

0

That's because you don't have any address_line_1 or address_line_2 in your array validation but you have posted those two elements.

You only have address.0.address_line_1 and there's no address.0.address_line_1 inside your posted data.

The elements of your array validation should be the same with what you posted. In your case, you can remove address.0.address_line_1 and add 2 more elements of your array validation.

'address_line_1' => 'required',
'address_line_2' => 'required',

The updated answer :

'address' => 'required|array',
'address.*' => 'required|array',
'address.*.address_line_1' => 'required|string',
Sign up to request clarification or add additional context in comments.

9 Comments

But my form fields look like: address[0]['address_line_1']
Try to change 'address.0.address_line_1' with 'address.*.address_line_1'
Please change the description of your posted array to posted address array because I thought that it was your full posted array.
tried this already, but get the same...
I've just posted the full array which is returned
|
0

Just replace your validator with this

$this->validate($request, [
   'first_name' => 'required',
   'last_name'  => 'required',
   'telephone'  => 'required',
   'email' => 'unique:contacts,email,' . $request->id,
   'address' => 'required',
    'address.*' => 'required',
    'address.*.address_line_1' => 'required',
]);

2 Comments

Thanks, but it's still failing validation saying "The address.0.address_line_1 field is required." even though it's filled.
I'm thinking this could possibly b a bug with Laravel version 5.2.45
0

I managed to figure out what was causing the issue...it was actually to do with the form input field names.

I removed the single quotes from the input names and it worked!

I changed:

<input type="text" class="form-control" name="address[0]['address_line_1']">

To, this:

<input type="text" class="form-control" name="address[0][address_line_1]">

And it worked! Hope it helps someone else.

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.