Validate array of inputs in form in Laravel 5.7
Asked Answered
L

6

5

My form has the same input field multiple times. My form field is as follows:

<input type='text' name='items[]'>
<input type='text' name='items[]'>
<input type='text' name='items[]'>

And request contains ($request['items'):

array:1 [▼
  "items" => array:3 [▼
    0 => "item one"
    1 => "item two"
    2 => "item three"
  ]
]

I want atleast one of the items to be filled. My current validation in the controller is

    $validator = Validator::make($request->all(),[
        'items.*' => 'required|array|size:1'
    ]);

It does not work. I tried with combination of size, required, nullable. Nothing works.

Label answered 19/9, 2018 at 11:10 Comment(6)
you want this fill in your database ? and check all requirements right ?Pellerin
Yes. The above code is in my controller.Label
possible duplicate of #45758949Analyze
Try without .* and change size to min as 'items' => 'required|array|min:1'Fatherless
I tried the solution, did not workLabel
why don't you post your controller code tooBicycle
P
7

In fact, it's enough to use:

$validator = Validator::make($request->all(),[
        'items' => 'required|array'
    ]);

The changes made:

  • use items instead of items.* - you want to set rule of general items, if you use items.* it means you apply rule to each sent element of array separately
  • removed size:1 because it would mean you want to have exactly one element sent (and you want at least one). You don't need it at all because you have required rule. You can read documentation for required rule and you can read in there that empty array would case that required rule will fail, so this required rule for array makes that array should have at least 1 element, so you don't need min:1 or size:1 at all
Piston answered 19/9, 2018 at 21:54 Comment(0)
I
3

You can check it like this:

$validator = Validator::make($request->all(), [
    "items"    => "required|array|min:1",
    "items.*"  => "required|string|distinct|min:1",
]);

In the example above:

  • "items" must be an array with at least 1 elements.
  • Values in the "items" array must be distinct (unique) strings, at least 1 characters long.
Inhabitancy answered 19/9, 2018 at 11:28 Comment(3)
In this case, $validator->fails() returns true even when a field is not emptyLabel
Are you entering the distinct field value?Inhabitancy
"items" => array:4 [▼ 0 => null 1 => null 2 => "item-three" 3 => null ] This is my inputLabel
I
1

You can use a custom rule with a closure.

https://laravel.com/docs/5.7/validation#custom-validation-rules

To check if an array has all null values check it with array_filter which returns false if they're all null.

So something like...

  $request->validate([

    'items' => [
      // $attribute = 'items', $value = items array, $fail = error message as string
       function($attribute, $value, $fail) {
         if (!array_filter($value)) {
           $fail($attribute.' is empty.');
         } 
       },
     ]
   ]);

This will set the error message: 'items is empty."

Inbeing answered 19/9, 2018 at 14:10 Comment(0)
I
1

Knowing you are using the latest version of Laravel, I really suggest looking into Form Request feature. That way you can decouple validation from your controller keeping it much cleaner.

Anyways as the answer above me suggested, it should be sufficient for you to go with:

'items' => 'required|array'
Ineradicable answered 19/9, 2018 at 22:0 Comment(0)
B
0

Just Do it normally as you always do:

 $validator = Validator::make($request->all(),[
    'items' => 'required'
  ]);
Bicycle answered 19/9, 2018 at 11:24 Comment(3)
Does not work as well. $validator->fails() returns false all the time for that conditionLabel
You mean $validator->fails() always returns false even if you pass an item in itemsBicycle
It's working fine in my project, there might be some thing wrong in your code, But I have laravel 5.6 in my project but you have 5.7 that might be the problem or something else.Bicycle
M
0

You should try this:

$validator = $request->validate([
    "items"    => "required|array|min:3",
    "items.*"  => "required|string|distinct|min:3",
]);
Mosemoseley answered 19/9, 2018 at 11:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.