I’m trying to set the prefix path for Zend Form’s validators and filters automatically. I’ve done a lot of research and found many solutions, but I want these to always take effect, automatically. I know I can do this in the form:
$this->addElementPrefixPath('My_Validate', 'My/Validate/', 'validate');
or set them on an element such as
$input->addValidatorPrefixPath('Other_Namespace', 'Other/Namespace');
$input->addFilterPrefixPath('Foo_Namespace', 'Foo/Namespace');
but is there any way for these to automatically look into what’s already set in the autoloader and/or be set in the bootstrap (or elsewhere), without having to set it ever again?
Here’s my autoloader:
// Autoload libraries
$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->registerNamespace('Lib1_')
->registerNamespace('Lib2_')
->registerNamespace('Lib3_');
Now, when I add a validator using
->addValidator('CustomValidator', false, 1)
I would like it to respect the hierarchy listed in the autoloader, falling back to Zend after that. I just haven’t been able to find how to automatically bootstrap this kind of autoloading for the validators and filters.
Thanks!
The way I get settings like this into all of my Zend forms without having to repeat the code all over for each form is to create a base form class that extends Zend_Form, which in turn all of my other forms extend.
In the base form’s constructor, I set up different decorators for various types of elements or customize the decorators for my application, specify prefix paths for helpers and validators and other things.
The important thing to note about this method, is you must call
parent::__construct()as the very last line if your base form __construct method. The reason for this is because theZend_Form::init()method gets called byZend_Form::__construct()and nothing else from the constructor runs after that.Here is an example: