Multiple array validation in laravel
Asked Answered
A

2

10

I have 3 types of data to validate

  1. data in group
  2. single data
  3. single and data in group combined

This validation works for single data

$validator = Validator::make($request->all(), [
    'tests.*.finding' => 'required',//works for single test
]);

Data sample for above

["tests"=>
                [
                    0 => ["finding"=>""]
                ],
                [
                    1 => ["finding"=>""]
                ]
            ]

And this validation works for data in group

$validator = Validator::make($request->all(), [
    'tests.*.*.finding' => 'required',//works for group
]);

Data sample for above

  ["tests"=>
                    [
                        "A" =>[
                            [
                                0 => ["finding"=>""]
                            ],
                            [
                                1 => ["finding"=>""]
                            ]
                        ],
                        "B" =>[
                            [
                                0 => ["finding"=>""]
                            ],
                            [
                                1 => ["finding"=>""]
                            ]
                        ]
                    ]
                ]

How to validate for single and data in group combined

Combined Data sample

 ["tests"=>
                    [
                        "A" =>[
                            [
                                0 => ["finding"=>""]
                            ],
                            [
                                1 => ["finding"=>""]
                            ]
                        ]
                    ],
                    [
                        0 => ["finding"=>""]
                    ],
                    [
                        1 => ["finding"=>""]
                    ]
                ]

Please help me to fix this, as 1st scenario always gives error for scenario second and vice versa.

Argument answered 17/3, 2017 at 14:13 Comment(1)
Probably can't do this with the validation rules. You can create a custom validator or split the Combined Data into the previous 2 examples if its easier.Sacramentalist
A
8

This is the solution,Laravel provided sometimes rule to manage existence of element and then only proceed to check next rule.

So final validation rule is.

 $validator = Validator::make($request->all(), [
 'tests.*.*.finding' => 'sometimes|required',//works for group
 'tests.*.finding' => 'sometimes|required',//works for single test
 ]);

Doc for this : https://laravel.com/docs/5.4/validation#conditionally-adding-rules

Argument answered 23/3, 2017 at 16:41 Comment(1)
Great! Glad you found a solution, interesting case you got there.Sacramentalist
N
2

You can following code to fix the validation data.

$customFieldValidation = ["test_id" => 'required|numeric',
        "test_two.id" => 'required|numeric',        
        ]);     
  $this->setRules ( $customFieldValidation );  
  $customeAttributes = [
  "test_three.*.id" => 'Test Three ' // custom message
  ];
  $this->setCustomAttributes ($customeAttributes);
  $this->_validate ();

I hope it's helpful to you.

Norval answered 23/3, 2017 at 7:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.