2

I'm having issues with double nested validation

My form is rather large and contains some nested data. Two of the fields:

{!! Form::text('address[city]', null, [] !!}
{!! Form::text('address[country[printable_name]]', null, [] )) !!}

For example this works:

'address.city' => 'required|max:255',

but

'address.country.printable_name' => 'required|max:255|country

throws "The address.country.printable name field is required." even though it has a valid country.

If I try to print all with $request->all() I get the following:

...,"address":{"city":"Maribor","country[printable_name":"Slovenia"},...

So there is missing ] after printable_name.

If I try to print

$request->input('address.country.printable_name')

I don't get anything, but it works when I try this:

$request->input('address')["country[printable_name"]

Did I do something wrong, is this not supported in Laravel or a bug? Either way, how can I get it work?

A workaround would be this

'boat.country[printable_name' => 'required|max:255',

but if I leave this the next developer to look at the code will probably want to kick my ass.

1 Answer 1

3

If you want to nest array items in request parameters you should do it like this:

{!! Form::text('address[country][printable_name]', null, []) !!}

Then you can access them as you've initially tried:

$request->input('address.country.printable_name')

Just think of structuring it as you would access it in an associative array in PHP. If you pass a parameter with this name in your form:

address[country][printable_name]

Then using plain PHP you would access it like this:

$_REQUEST['address']['country']['printable_name'];

The above example illustrates the equivalent structure.

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

3 Comments

Try it, address.country.printable_name won't return anything. I know it should, but it doesn't.
Well what is the output of dump($request->all()) after you've made the change I've suggested?
Sorry, made a typo when I first tried your way. Works like a charm. Thanks!

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.