在Zend上的Element中添加验证器2次

时间:2011-11-10 13:07:22

标签: php zend-framework zend-form zend-form-element zend-validate

我创建了一个Validator,它接收一个类实例和一个函数名,然后执行它。

这对于通过数据库表格映射器执行许多自定义验证非常有用。

问题是我无法多次将此验证器添加到Element中,我需要这样做...

示例:

我有一个包含用户名和密码元素的登录表单。对于用户名,我需要通过此验证器验证它是否已存在于数据库中,如果存在,我需要验证所选帐户是否已激活...

我知道我只能创建一个函数来接收两者的结果但是,我的Validator应该只返回一条错误消息......

以下是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;
}

我该如何解决这个问题?有没有办法用动态名称在PHP中创建一个类?有没有办法将相同的类Validator添加到Zend中的元素,还是应该更改我的Valitor以返回多条消息并在构造函数中传递它们?

1 个答案:

答案 0 :(得分:0)

Kees Schepers在评论中提供的解决方案应该可行,但是,当我自己执行“Zend_Validate_Callback”时,我可以改变其工作方式以允许定义多个消息。

现在它运作良好=)

无论如何,谢谢Kees Schepers。下次,我将使用Zend_Validate_Callback而不是做更多的工作x)。

相关问题