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.

panthro
  • 22,779
  • 66
  • 183
  • 324
  • 2
    If you dump out the `$request->input()` what are the key names of your input? –  Aug 21 '19 at 09:38
  • Possible duplicate of [How to validate array in Laravel?](https://stackoverflow.com/questions/42258185/how-to-validate-array-in-laravel) – dparoli Aug 21 '19 at 09:40
  • "users" => array:1 [▼ 0 => array:2 [▼ "name" => "sdfa" "date" => "sadf" ] ] – panthro Aug 21 '19 at 09:41
  • Can you share the code you use to validate, rather than just the rule? –  Aug 21 '19 at 09:50
  • It's just a form request. nothing to show. – panthro Aug 21 '19 at 09:51
  • I'm trying to help you mate, can you edit your question to add the code when you do your validation please. Are you doing $request->validate(), are you making a validator??? Are any of the values unique? There can be multiple reasons. –  Aug 21 '19 at 09:52
  • Please explain why this is not a duplicate and show us more details, as posted is equal to any other question on array validation. – dparoli Aug 21 '19 at 09:54
  • No im not making a validator, im using a form request. – panthro Aug 21 '19 at 09:56
  • Please can you just post your code, it's not difficult. No code, no help. simple. –  Aug 21 '19 at 09:56
  • [Here is what I have found](https://stackoverflow.com/questions/45217748/laravel-array-validation-in-form-request) –  Aug 21 '19 at 10:01
  • Outdated question and answer, array validation have changed in laravel since then. – panthro Aug 21 '19 at 10:02
  • Then follow the [documentation](https://laravel.com/docs/5.8/validation#validating-arrays) and adapt it, or just make a validator. The documentation wouldn't show it if it doesn't work. –  Aug 21 '19 at 10:05
  • I think the issue is that the documentation doesnt show how to name your fields correctly – panthro Aug 21 '19 at 10:08

1 Answers1

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]">