0

I created a my own validation class under /library/My/Validate/

In my form I have $this->addElementPrefixPath('My_Validate', 'My/Validate', 'validate');

I am using my validator like so:

$this->addElement('text', 'aField', array(
    'validators' => array(
        array('TestValidator', false, array('messages' => 'test failed')
    ), 
));

This all works. However, I am interested in improving this in two ways.

  1. I would like to make it so that all forms have access to my validator. Calling addElementPrefixPath() in every form doesn't seem to be a clean way of doing this.

  2. I would like to pass in My_Validate_TestValidator instead of TestValidator so other developers know what they are working with right away.

ryy
  • 317
  • 1
  • 3
  • 13

1 Answers1

0

To answer your first question, the only real easy way to do this would be to create your own instance of the form - My_Form_Abstract - which has an init() method that sets the prefix path - and then of course calls the parent init().

I'm not aware of a way to make your second method work flawlessly. You need to store a prefix in order to build the validator loader correctly. However, as an alternative, you might try creating new instances of the class using the full name, and then adding it to the element:

$element = $this->getElement('aField');
$myValidateTestValidator = new My_Validate_TestValidator();
$element->addValidator($myValidateTestValidator);
Aaron Saray
  • 1,178
  • 6
  • 19