Zend framework 在Zend中为电子邮件字段设置单个错误消息
我在Zend表单中面临一个关于电子邮件验证的小问题 我的电子邮件字段代码如下 目前,它将电子邮件错误消息显示为: 我想要的是在所有这些错误的位置设置一个单一错误消息,如下所示: “'abcd@shdsjah'不是有效的电子邮件Id。“ 因为我是Zend框架的新手,所以我对它没有太多的想法,虽然我尝试了一些代码,但它们是无用的 请帮忙Zend framework 在Zend中为电子邮件字段设置单个错误消息,zend-framework,zend-form,zend-validate,Zend Framework,Zend Form,Zend Validate,我在Zend表单中面临一个关于电子邮件验证的小问题 我的电子邮件字段代码如下 目前,它将电子邮件错误消息显示为: 我想要的是在所有这些错误的位置设置一个单一错误消息,如下所示: “'abcd@shdsjah'不是有效的电子邮件Id。“ 因为我是Zend框架的新手,所以我对它没有太多的想法,虽然我尝试了一些代码,但它们是无用的 请帮忙 提前感谢……我看到您正在通过自己的自定义验证器 ->addValidator(new Validator_EmailValidator()) 你不需要那样
提前感谢……我看到您正在通过自己的自定义验证器
->addValidator(new Validator_EmailValidator())
你不需要那样做。只需使用:
$validator = new Zend_Validate_EmailAddress()
然后在表单项上设置该验证器,然后针对该验证器设置消息
所以
现在只需使用setMessages
方法针对验证器设置消息
以下是您可以更改的所有潜在消息:
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';
消息默认值 现在只需将消息更改为您想要的任何内容。您需要更新每条消息
$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",
));
当我刚接触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."));
在您的代码之后编写此代码
我希望它能对你有所帮助 将true作为addValidator的第二个参数传递(breakChainOnFailure)。验证将在第一次失败时停止,您将只有一条错误消息。我已经尝试过了,但上图中显示的第二条和第三条消息仍然会出现……?这很好,但是如果您有多个验证器,每个验证器都有一条自定义消息,该怎么办?添加这条消息将覆盖来自其他验证器的所有其他错误消息。
$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",
));
//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."));