时间:2010-07-24 20:56:21

标签: php zend-framework zend-form

3 个答案:

答案 0 :(得分:6)

答案 1 :(得分:0)

您需要创建自定义错误类。就我而言:

我在库/ Zend / Validate文件夹中创建了自定义电子邮件验证类

class Zend_Validate_Email extends Zend_Validate_Abstract
{

    const INVALID = 'Email is required';
    protected $_messageTemplates = array(
    self::INVALID => "Invalid Email Address",
   );

     public function isValid($value)
     {
         if(filter_var($value, FILTER_VALIDATE_EMAIL) === false) {
            $this->_error(self::INVALID);
            return false;
        } else {
            // We only check the presence of a dot on the domain part
            $components = explode("@", $value);
            $domain = $components[1];

            if (strpos($domain, ".") === false) {
                $this->_error(self::INVALID);
                return false;
            }

            return true;
        }
     }
}

并在我的表单中用作

$email_validate = new Zend_Validate_Email();

$neValidator = new Zend_Validate_NotEmpty();

$neValidator->setMessage('Please enter email.');

$emailaddress = new Zend_Form_Element_Text('EmailAddress');

$emailaddress->setRequired(true)

          ->setAttrib('size', '30')

          ->addFilter('StripTags')

          ->setAttrib('MaxLength',100)

          ->addValidator($neValidator,TRUE)

              ->addValidator($email_validate,TRUE)

          ->setAttrib('class','textbox')

          ->setDecorators($decorators);

希望它能帮到你

答案 2 :(得分:0)

$email_validate = new Zend_Validate_Email();
$neValidator = new Zend_Validate_NotEmpty();
$neValidator->setMessage('Please enter email.');
$emailaddress = new Zend_Form_Element_Text('EmailAddress');
$emailaddress->setRequired(true)