Hello everybody!
I've been recently working a lot with Zend_Form and validators and startet
using validator chains. I want to use the same validators at two different
places (a Zend_Form and an individual class) to check values and it seemed a
good way to pack every validator of an element in a validator chain.
Nearly everything works as expected.
-- The sample:
// Creating the validator chain
$validatorChain = new Zend_Validate();
$validatorChain->addValidator(new Zend_Validate_NotEmpty());
$validatorChain->addValidator(new Zend_Validate_Digits());
// Applying the validator chain on an Zend_Form_Element
$formElement = new Zend_Form_Element_Text('name');
$formElement->addValidator($validatorChain);
// Additional check (e.g. before saving to the database)
if (!$validatorChain->isValid($value))
throw new Exception('validation error: ' . implode("\n",
$validatorChain->getMessages()));
So far no problem.
-- And now the problem:
For my project, it was necessary to write custom validators that work with
the $context argument of the isValid() method. The problem is, that it is -
by default - not possible to pass the context to the validator chain,
because the Zend_Validate::isValid() simply doesn't have a second argument
so it can't pass it to the isValid() method of the validator.
-- The question:
How - generally - can i solve this specific problem? I could write my own
class that extends Zend_Validate and simply implement this feature but I am
looking for something like the "best practice" strategy. There could be a
completely different approach to solving the problem of same checks in
different locations I haven't thought about yet.
Thanks for your time and especially your opinion on the subject.
with best regards Simon Plangger
--
View this message in context:
http://zend-framework-community.634137.n4.nabble.com/validator-chains-and-context-tp2130875p2130875.html
Sent from the Zend Framework mailing list archive at Nabble.com.