简体   繁体   English

Zend框架中的自定义验证程序问题

[英]Custom Validator Issue in Zend Framework

I have form called LoginForm, which extends the RecipeForm which in turn extends Zend_Form. 我有一个名为LoginForm的表单,该表单扩展了RecipeForm,而后者又扩展了Zend_Form。 The RecipeFrom only returns my decorators. RecipeFrom仅返回我的装饰器。

When the form is submitted, I get the following error, 'Message: Method addValidator does not exist'. 提交表单后,出现以下错误,“消息:方法addValidator不存在”。

class Recipe_Form_LoginForm extends Recipe_Form_RecipeForm {
    public function init()
    {
        parent::init();
        $this->setName('loginform')
             ->setAction('/login');


        //  Add Email Element
        $email = $this->addElement('text', 'email', array(
            'label' => 'Email Addresses',
            'required'=> true,
            'size'=>12,
            'filters'=>array('StringTrim'),
            'decorators' => $this->getElementDecorator('email'),

        ));
       $email->addValidator(new Recipe_Validate_EmailAddress(), true, array(
           'messages' => array(
               Recipe_Validate_EmailAddress::INVALID => 
               'Please enter email in correct format',
               Recipe_Validate_EmailAddress::EMAILISEMPTY =>
               'Please enter email address'
       )));
}

class Recipe_Validate_EmailAddress extends Zend_Validate_Abstract
{
    const INVALID           =   'notvalid';
    const EMAILISEMPTY      =   'isempty';

     protected $_messageTemplates = array(
             self::INVALID => "Email is in invalid format",
             self::EMAILISEMPTY => "You have to fill email field"
        );

    public function isValid($value){
        $response = parent::isValid($value);
        if(!$response){
            $this->_message = array(self::INVALID => "Please enter a valid email address");
        }
        return $response;
    }
}
?>

When you call $this->addElement() from within a Zend_Form object, it returns the form object itself, rather than the element you just created. 当您从Zend_Form对象中调用$this->addElement() ,它将返回表单对象本身,而不是您刚创建的元素。

You can make one of the following changes: 您可以进行以下更改之一:

$this->addElement('text', 'email', ...);
$email = $this->getElement('email');
$email->addValidator(...);

// or

$email = new Zend_Form_Element_Text('email');
$email->addValidator(...)
      ->setLabel(...)
      ->setRequired(...);
$this->addElement($email);

To set your error message, I think you should do this instead of setting $this->_message. 要设置错误消息,我认为您应该这样做而不是设置$ this-> _ message。

$this->_error(self::INVALID);

Since it looks like your class is only extending Zend's email validator to override the message, you can override Zend's messages like this and not need to extend the class. 由于看起来您的班级只是扩展Zend的电子邮件验证程序以覆盖消息,因此您可以像这样覆盖Zend的消息,而无需扩展类。 This is taken from a validator in one of my projects, so ignore the extra stuff and just pay attention to the messages for the EmailAddress validator. 这是从我的一个项目的验证器中获取的,因此请忽略多余的内容,而只需注意EmailAddress验证器的消息。

$this->addElement('text', 'email', array(
        'label' => 'Email Address:',
        'required' => false,
        'filters' => array('StringTrim', 'StringToLower'),
        'validators' => array(
            array('EmailAddress', true, array(
                'messages' => array(
                    Zend_Validate_EmailAddress::INVALID_FORMAT =>
                    "'%value%' is not a valid email address. Example: you@yourdomain.com",
                    Zend_Validate_EmailAddress::INVALID_HOSTNAME =>
                    "'%hostname%' is not a valid hostname for email address '%value%'"
                )
            )),
            array('Db_RecordExists', true, array(
                'table' => 'accounts', 'field' => 'email',
                'messages' => array(
                    Zend_Validate_Db_RecordExists::ERROR_NO_RECORD_FOUND =>
                    "No account with that email address was found"
                )
            ))
        ),
        'decorators' => $this->elementDecorators
    ));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM