Search code examples
phpzend-frameworkzend-formzend-form-elementzend-validate

Add a Validator 2 times in an Element on Zend


I made a Validator that receives a class instance and a function name and then executes it.

This is usefull for execute many custom validations through database table mappers.

The problem is that I can't add this validator more than one time to a Element and I need to do it...

Example:

I've a login form that have a username and password elements. For username, I need to verify via this Validator if it already exists in the Database and, if it exists, I need to verify if the selected account is activated...

I know that I could make just one function to receive the result for both but, my Validator should only return one error message...

Here's the constructor of the Validator:

/**
 *$class is a class instance
 *$function is the function name to execute in the class
 *$invalidReturns specifies when the validator should return the error message (default is when the function returns null, false or empty string)
 *$tokens are used as parameters of the function to execute
*/
public function __construct($class, $function, $invalidReturns=null, $tokens=null)
{
    $this->class = $class;
    $this->function = $function;
    $this->tokens = $tokens;
    $this->returns = $invalidReturns;
}

How can I solve this problem? Is there a way to create a Class in PHP with dynamic name? Is there a way to add a same class Validator to an element in Zend or should I change my Valitor to return more then one message and pass them in the constructor?


Solution

  • The solution provided by Kees Schepers in the comments should work but, as I did my own "Zend_Validate_Callback", I could change the way it works to allow the definition of more than one message.

    Now it's working well =)

    Anyway, thanks Kees Schepers. Next time, I will use Zend_Validate_Callback instead of doing a lot of more work x).