I'm on "zendframework/zend-inputfilter" v2.8.1. Quite surprisingly for me - I wasn't able to find the configuration or setup option for next scenario:
We have 2 fields, and either one or another needs to be populated.
For example: we have 2 fields: "year" and "age". I want to create a validation config where either "year" or "age" needs to be set.
So my valid payload should look like this
{
"year": 2000
}
Or
{
"age": 18
}
Or
{
"year": 2000,
"age": 18
}
This should be invalid:
{}
Unfortunately it seems other questions like this (Conditionally required in Zend Framework's 2 InputFilter) are either outdated or not accurate. For example if I try to make the field "year" optional, for payloads
{
"age": 18
}
and
{}
it's getting skipped because of this code in \Zend\InputFilter\BaseInputFilter::validateInputs
// If input is optional (not required), and value is not set, then ignore.
if (! array_key_exists($name, $data)
&& ! $input->isRequired()
) {
continue;
}
If I make it required, I'm hitting condition in \Zend\InputFilter\Input::isValid
if (! $hasValue && $required) {
if ($this->errorMessage === null) {
$this->errorMessage = $this->prepareRequiredValidationFailureMessage();
}
return false;
}
I'm hesitant to override the BaseInputFilter or Input classes, but from what I can see right now that seems like my only option. However maybe I'm missing something. I'll appreciate any advice, thanks!