1

First and foremost:

I am trying to validate an array containing input[text]. They are defined currently as:

<input type='text' name='user[0][name]'/><div>@error('user[0][name]'){{ $message }}@enderror</div>
<input type='text' name='user[1][name]'/><div>@error('user[1][name]'){{ $message }}@enderror</div>

I tried the 3 variants below as well:

<input type='text' name='user[][name]'/>
<input type='text' name='name[]'/>
<input type='text' name='name[0]'/>

My ExampleController does this, in the store() method:

    $validator = Validator::make($request->all(), [
        'user.*.name' => 'required|string',
    ])->validate();

I've also tried using:

    $validatedData = $request->validate([
        "user.*.name"    => "required|string",
    ]);//*/

The other option that I've tried to use to match was (for the other case):

    'name.*' => 'required|string',

None of these manage to print an error message in the div that follows the input.

The only way for me to get to see the error, is if I do the validation in of the two below (for each case):

"user[0][name]"    => "required|string",
"name[0]"          => "required|string",

So... what is it that I'm doing wrong?

1 Answer 1

2

user[0][name] is the correct syntax for naming your form input elements.

To access the error message(s), however, use dot notation: user.0.name.

Here's a working playground.

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

1 Comment

That was it. 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.