在Zend中为电子邮件字段设置单个错误消息

时间:2011-09-01 11:49:31

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

我在Zend表单中遇到一个关于电子邮件验证的小问题。

我的电子邮件字段代码为

$emailId = new Zend_Form_Element_Text('email');
$emailId->setLabel("Email Adresse")
         ->addFilter('StripTags')
         ->addFilter('StringTrim')
         ->addValidator(new Validator_EmailValidator())
         ->addValidator('NotEmpty')
         ->addValidator(
                        'NotEmpty',
                        TRUE,
                        array('messages' => array(
                              'isEmpty' => 'Please enter your email id.'
                              )
                           )
                        );

目前它将电子邮件错误消息显示为: enter image description here

我想要的是在所有这些错误的地方设置单一错误消息,即:

“'abcd @ shdsjah'不是有效的电子邮件ID。”

由于我是Zend Framework的新手,我对它并不太了解,虽然我尝试了一些代码,但它们没用。

请帮助.....

提前致谢....

3 个答案:

答案 0 :(得分:4)

当我刚接触zend-framework时,我遇到了这个问题并通过 setErrors()方法得到了解决方案:

//this will immediately call the method markAsError() which will show the error always
$emailId->setErrors(array('Please enter a valid Email Id.'));

您也可以尝试:

//this will clearErrorMessages() and after that set the error messages 
$emailId->setErrorMessages(array("Please enter a valid Email Id."));

在代码后面编写此代码。

我希望它对你有帮助......

答案 1 :(得分:1)

传递true作为addValidator的第二个参数(breakChainOnFailure)。验证将在第一次失败时停止,您将只有一条错误消息。

答案 2 :(得分:0)

我看到你传递了自己的自定义验证器。

->addValidator(new Validator_EmailValidator())

你不需要这样做。只需使用:

 $validator =  new Zend_Validate_EmailAddress()

然后在表单项上设置该验证器,然后针对该验证器设置消息。

所以

 $emailId->setValidator( $validator );

现在只需使用setMessages方法对Validator设置消息。

这些是您可以更改的所有潜在消息:

    const INVALID            = 'emailAddressInvalid';
    const INVALID_FORMAT     = 'emailAddressInvalidFormat';
    const INVALID_HOSTNAME   = 'emailAddressInvalidHostname';
    const INVALID_MX_RECORD  = 'emailAddressInvalidMxRecord';
    const INVALID_SEGMENT    = 'emailAddressInvalidSegment';
    const DOT_ATOM           = 'emailAddressDotAtom';
    const QUOTED_STRING      = 'emailAddressQuotedString';
    const INVALID_LOCAL_PART = 'emailAddressInvalidLocalPart';
    const LENGTH_EXCEEDED    = 'emailAddressLengthExceeded';

消息默认值

protected $_messageTemplates = array(
        self::INVALID            => "Invalid type given. String expected",
        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",
    );

现在只需将消息更改为您想要的任何内容即可。您需要更新每条消息。

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