1

I've seen how to validate arrays in the docs. Using something like:

class UsersRequest extends FormRequest
{
    public function rules()
    {
        'users.*.name' => 'required',

    }
}

On my form I have the field named:

<input name="users[0][name]">

This is actually looped out, but have statically added it in above example.

The issue I have is that the above will validate, so if there is no value in the field, it will error, and with a value, it will pass, unfortunately the input does not have an error class on it, as I suspect the name in the error bag is different to the name on the input.

How can I correctly validate / correctly name array inputs in laravel?

Edit.

This is not a duplicate, have tried other answer on SO and they do not work.

13
  • 2
    If you dump out the $request->input() what are the key names of your input? Commented Aug 21, 2019 at 9:38
  • Possible duplicate of How to validate array in Laravel? Commented Aug 21, 2019 at 9:40
  • "users" => array:1 [▼ 0 => array:2 [▼ "name" => "sdfa" "date" => "sadf" ] ] Commented Aug 21, 2019 at 9:41
  • Can you share the code you use to validate, rather than just the rule? Commented Aug 21, 2019 at 9:50
  • It's just a form request. nothing to show. Commented Aug 21, 2019 at 9:51

1 Answer 1

1

I usually do like this. It's very simple:

<input id="users.0.name" name="users[0][name]">

Edit: Correction is it's not required to set an Id attribute until you need them yours is fine:

 <input name="users[0][name]">
Sign up to request clarification or add additional context in comments.

3 Comments

in error bag the name should be like "users.0.name" which is not friendly to view though, you can write custom message at lang specific for them
that is annoying as my inputs check errors based on their name
@panthro I have written custom messages to view friendly messages. But it's obvious by design they will call through those pattern name until you override that pattern

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.