Checking multiple arrays in laravel

I have 3 types of data to check

  • group data
  • single data
  • single and data in a combined group

This check works for single data.

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

Example data above

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

And this check works for the data in the group

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

Example data above

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

How to check for single and data in a combined group

Combined Data Example

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

Please help me fix this, since the first script always gives an error for the second script and vice versa.

+7
php validation laravel laravel-5 laravel-validation
source share
2 answers

This solution, Laravel sometimes provided a rule to control the existence of an element, and then only proceed to verify the next rule.

Thus, the final verification rule.

  $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

+4
source share

You can execute the code to correct the verification 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 this helps you.

+2
source share

All Articles