Reputation: 2721
This is strange. I'm trying to set the error message for my email validation element in Zend form. Other fields are working correctly-but not email-what's up?
code bit:
$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.');
Name works, but email doesn't work.
I am still getting the defauly error message: '' is no valid email address in the basic format local-part@hostname
Upvotes: 1
Views: 7047
Reputation: 2639
you need to create custom error class. In my case:
I have created custom email validation class in library/Zend/Validate folder
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;
}
}
}
and used in my form as
$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);
Hope it will help you
Upvotes: 0
Reputation: 1
$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)
Upvotes: 0
Reputation: 6202
You need to specify the message type:
$email->getValidator('emailAddress')->setMessage("'%value%' is not valid, use something like local-part@hostname",Zend_Validate_EmailAddress::INVALID_FORMAT);
Take this as example:
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",
);
Upvotes: 6