Validation
Validation copied to clipboard
Conditional Validation: More then "when"
When is a great rule for a binary case. Is there a rule or way to do conditional validation if the conditional key value can have more than two outcomes? e.g.
$cars = [
['manufacturer' => 'Honda', 'model' => 'Accord'],
['manufacturer' => 'Toyota', 'model' => 'Rav4'],
['manufacturer' => 'Ford', 'model' => 'notarealcar']
];
Validator::arrayType()->each(
Validator::key('manufacturer', Validator::in(['Honda', 'Toyota', 'Ford']))
->when(
Validator::key('manufacturer', Validator::equals('Honda')),
Validator::key('model', Validator::in(['Accord', 'Fit'])),
Validator::when(
Validator::key('manufacturer', Validator::equals('Toyota')),
Validator::key('model', Validator::in(['Rav4', 'Camry'])),
Validator::key('model', Validator::in(['F150', 'Bronco']))
)
)
)->assert($cars);
This code works, but chaining 'when' gets a little cumbersome after a few 'manufacturers'. Is there some kind of switch-like rule?