I have a registration input that can be used by a consumer or a fleet consumer. Depending on what they are, depends on if they send extra details about their fleet or not. A regular consumer will just send their information, where as a fleet consumer will send the same information, with 2 extra fields in a sub array.
The input data is:
// POST data registration
// First example, fleet
{
"firstName" : "Mike",
"lastName" : "Dan",
"email" : "email@email.com",
"password" : "rawpassword",
"fleet" : {
"fleetCompanyName" : "My Company",
"driverID" : "1234567890"
}
}
// Second example, non fleet
{
"firstName" : "Mike",
"lastName" : "Dan",
"email" : "email@email.com",
"password" : "rawpassword",
}
I need to validate the fleet input if it is set (Both fields need to be valid if its set), but ignore the fleet field if it is not set.
# Validation
$v = v::key('firstName', v::notEmpty())
->key('lastName', v::notEmpty())
->key('email', v::notEmpty())
->key('password', v::notEmpty())
->key('fleet', v::optional(v::arrayType(),[v::key('fleetCompanyName',v::charset('ASCII')),v::key('driverID', v::intVal())]),false);
That's my current iteration, which allows fleet to be optional, but isn't working on validating the array correctly.
Any help would be greatly appreciated.
Cheers,
Phil
I was unable to do it in one command, so found this solution for it:
$v = v::key('firstName', v::notEmpty())
->key('lastName', v::notEmpty())
->key('email', v::notEmpty())
->key('password', v::notEmpty())
->key('fleet', v::optional(v::key('fleetCompanyName', v::charset('ASCII'))), false)
->key('fleet', v::optional(v::key('driverID', v::intVal())), false);
For each sub array value I need, I would need to add another command to the chain like the above. Hope that helps anyone else.