Created
August 14, 2024 10:23
-
-
Save jovialcore/73b795276515c5347d9f2c751433819a to your computer and use it in GitHub Desktop.
my template for Valdiating array of arrays properly in laravel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$validator = Validator::make( | |
$request->all(), | |
[ | |
// you need to pass the question key so that it acts as a validator to make sure we have the array contents. | |
// if the question key is not added and the form requests doesn't come with the array contents, the validation will be skipped | |
'question' => 'required', | |
"question.*.id" => "required|integer", // This will validate each `question` array item | |
"question.*.applicant_answer" => "required|integer", // T | |
], | |
[ | |
'question.*.applicant_answer.required' => 'You need to answer the qestion', | |
'question.*.applicant_answer.integer' => 'Only Integers', | |
'question.*.id.required' => 'Attach the question', | |
'question.*.id.integer' => 'Only Integers' | |
] | |
); | |
if ($validator->fails()) { | |
$this->reason = implode('', $validator->errors()->all()); | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment