I'm trying to use Zend\Validator
to validate objects but I find it hard for a couple of reasons and am now wondering if I'm doing something fundamentally wrong or if the component is just not a good choice to do so…
Ideally I'd like to run
$objectValidator = new ObjectValidator();
$objectValidator->isValid($object);
So in this case I would put (sub-)validators for the objects properties in ObjectValidator
's isValid()
method for example like this:
public function isValid($value, $context = null)
{
$propertyValidator = new Zend\Validator\Callback(function($value) {
return false;
});
if (!$propertyValidator->isValid($value)) {
foreach ($propertyValidator->getMessages() as $code => $message) {
$this->abstractOptions['messages'][$code] = $message;
}
return false;
}
return true;
}
The way to merge the messages from the property's validator I've copied from the component's EmailAddress
validator that falls back on the Hostname
validator.
The trouble starts when I'm using a type of validator twice (e.g. Callback
) no matter if on the same property or a different since the messages are merged and I'm loosing information that I'd like to have. I could build a way to manage the messages myself but I'm wondering if there's not a better solution.
I also thought of using Zend\InputFilter
instead creating Zend\Input
s for each property that I want to run checks on. This way I certainly get all the messages but it adds a rather annoying task of dismantling the object before I can validate it.
Any input highly appreciated.