-- Dylan Arnold <[EMAIL PROTECTED]> wrote
(on Thursday, 28 February 2008, 05:19 PM +1300):
> Can somebody please give me an example of using a custom validator with
> Zend_Form?
>
> For the basic case using Zend_Validate_*:
> $element = new Zend_Form_Element_Text('test');
> $element->addValidator('Regex', ....);
> //works fine.
>
> However:
> $element->addValidator('Zend_Validate_Regex', ....);
> //doesn't work. I get an exception Plugin by name Zend_Validate_Regex was not
> found in the registry. What am I doing wrong?
By default, the plugin loader looks for a class that has one of the
registered prefixes. So, if you pass 'Regex' as the name, and
'Zend_Validate' is a registerd prefix, it finds it. However,
there is no match to a fragment 'Zend_Validate_Regex' with an
*additional* 'Zend_Validate' prefix.
So, either use the short name ('Regex', in your example), or instantiate
the validator:
$element->addValidator(new Zend_Validate_Regex($pattern));
> Now lets say I created a custom validator My_Validate_Test:
> $element->addPrefixPath('My_Validate', 'My/Validate/',
> Zend_Form_Element::VALIDATE);
> $element->addValidator('Test', ....);
> //This works
>
> However:
> $element->addValidator('My_Validate_Test', ...);
> //Doesn't work. (Exception Plugin by name My_Validate_Test was not found in
> the
> registry.
>
> Where am I going wrong?
Same problem as above. You now have the prefixes "Zend_Validate" and
"My_Validate" on the stack, and it's prepending whatever you pass with
those prefixes when doing the lookup.
> Also. Does $form->addElementPrefixPath('My_Validate', 'My/Validate/',
> Zend_Form_Element::VALIDATE); Add the path to all elements. I can only get the
> path to work by setting it for each individual element.
Should work on all elements... but only if they are created through your
form object (i.e., via createElement() or addElement()). If you're
instantiating your elements directly, you'd need to add the prefix path
to all elements individually, as they would have no form awareness until
added to the form.
--
Matthew Weier O'Phinney
PHP Developer | [EMAIL PROTECTED]
Zend - The PHP Company | http://www.zend.com/