I want to validate the values sent from a form that contains an undetermined amount of checkboxes.
These send an array of deploymentCustomers[] to the Request. I can validate that deploymentCustomers[] is an array but i don't know how i can validate each value in the array as i don't know what the array keys will be.
I'm not sure if i'm missing something but as i don't know what the key names will be i don't see how the dot notation will allow me to do it. I have created a function for creating rules for the unknown array keys.
public function validateDeployment()
{
$rules = $this->validateDeploymentRules();
return request()->validate($rules);
}
public function validateDeploymentRules()
{
$rules = [
'name' => ['required', 'string', 'max:255']
];
// Add rules for all the Add Customer checkboxes
$deploymentCustomers = request()->input('deploymentCustomers');
foreach ($deploymentCustomers as $index => $value){
$rules['deploymentCustomers.' . $index] = ['integer'];
}
return $rules;
}