views:

560

answers:

2

I'm writing a custom validator that is going to check for the existence of an email such that if it already exists in the database, the form is not valid. I'm having a hard time figuring out helper paths and namespaces for custom Zend_Validation classes.

I'd like to call the class My_Validate_EmailUnique but I keep getting error messages such as: there is an error exception 'Zend_Loader_PluginLoader_Exception' with message 'Plugin by name 'My_Validate_EmailUnique' was not found in the registry; used paths: My_Validate_: /var/www/site/arc/helpers/

The class looks like this:

    <?php
class My_Validate_EmailUnique extends Zend_Validate_Abstract
{
    const EMAIL_UNIQUE = 'notMatch';

Can someone help me with where I register for the Zend_Form to look for custom validators?

+1  A: 

+1 for Db_NoRecordExists - the docs have an example showing exactly what you want to do.

Otherwise, custom validators can be loaded like regular library classes, so try placing it on your include path.

/library/My/Validate/EmailUnique.php


You can also add a new entry to the Zend_Application_Module_Autoloader if you want to keep it in your application folder, as opposed to your library:

// Bootstrap.php
protected function _initAutoloader()
{
    $autoloader = new Zend_Application_Module_Autoloader(array(
        'namespace' => 'My_',
        'basePath'  => dirname(__FILE__),
    ));

    $autoloader->addResourceType('validator', 'validators', 'Validate')

    return $autoloader;
}

And put the class My_Validate_EmailUnique in:

/application/validators/EmailUnique.php

I've never registered a custom validator as a plugin before, so I can't help you there sorry.

Richard Nguyen
in terms of Db_NoRecordExists, how do you turn it off between an insert and an update. I use the same action (manageAction) and, on an update, I do not want it to be validated. It looks like I might be able to do a removeValidator but, ideally, I would like to be doing this not in the Controller but in the model (or possible a service). Any advice on if this is the right approach.
timpone
+1  A: 

You can directly add the validator to your form element. There is no need to write a custom validator for this.

In your form:



$element->addValidator(new Zend_Validate_Db_NoRecordExists('mytablename', 'myemailcolumnname'));
Sudheer
The fourth argument is useful if you want to define which `Zend_Db_Adapter` to use.
chelmertz