无法为电子邮件验证设置错误消息 - Zend-form

Joe*_*oel 1 php zend-framework zend-form

这很奇怪.我正在尝试以Zend格式为我的电子邮件验证元素设置错误消息.其他领域工作正常 - 但不是电子邮件 - 什么事?

代码位:

$name = new Zend_Form_Element_Text('name');
        $name   ->setLabel('Name:')
                ->setRequired(true)
                ->addValidator('Alnum')
                ->addValidator('NotEmpty');
        $name    ->getValidator('NotEmpty')->setMessage('Please enter your name.');
        $name    ->getValidator('Alnum')->setMessage('Name can only contain letters and spaces.');

$email = new Zend_Form_Element_Text('email');
       $email   ->setLabel('Email:')
             ->setRequired(true)
                ->addFilter('StringToLower')
                ->addValidator('NotEmpty')
                ->addValidator('EmailAddress');

        $email  ->getValidator('NotEmpty')->setMessage('Please enter your email address.');
        $email  ->getValidator('EmailAddress')->setMessage('Email is not in proper format.');
Run Code Online (Sandbox Code Playgroud)

名称有效,但电子邮件不起作用.

我仍然收到defauly错误消息: '' is no valid email address in the basic format local-part@hostname

Key*_*ana 6

您需要指定消息类型:

$email->getValidator('emailAddress')->setMessage("'%value%' is not valid, use something like local-part@hostname",Zend_Validate_EmailAddress::INVALID_FORMAT);
Run Code Online (Sandbox Code Playgroud)

以此为例:

protected $_messageTemplates = array(
        self::INVALID            => "Invalid type given, value should be a string",
        self::INVALID_FORMAT     => "'%value%' is no valid email address in the basic format local-part@hostname",
        self::INVALID_HOSTNAME   => "'%hostname%' is no valid hostname for email address '%value%'",
        self::INVALID_MX_RECORD  => "'%hostname%' does not appear to have a valid MX record for the email address '%value%'",
        self::INVALID_SEGMENT    => "'%hostname%' is not in a routable network segment. The email address '%value%' should not be resolved from public network.",
        self::DOT_ATOM           => "'%localPart%' can not be matched against dot-atom format",
        self::QUOTED_STRING      => "'%localPart%' can not be matched against quoted-string format",
        self::INVALID_LOCAL_PART => "'%localPart%' is no valid local part for email address '%value%'",
        self::LENGTH_EXCEEDED    => "'%value%' exceeds the allowed length",
    );
Run Code Online (Sandbox Code Playgroud)