我有一个名为 LoginForm 的表单,它扩展了 RecipeForm,而后者又扩展了 Zend_Form。RecipeFrom 只返回我的装饰器。
提交表单时,我收到以下错误,“消息:方法 addValidator 不存在”。
class Recipe_Form_LoginForm extends Recipe_Form_RecipeForm {
public function init()
{
parent::init();
$this->setName('loginform')
->setAction('/login');
// Add Email Element
$email = $this->addElement('text', 'email', array(
'label' => 'Email Addresses',
'required'=> true,
'size'=>12,
'filters'=>array('StringTrim'),
'decorators' => $this->getElementDecorator('email'),
));
$email->addValidator(new Recipe_Validate_EmailAddress(), true, array(
'messages' => array(
Recipe_Validate_EmailAddress::INVALID =>
'Please enter email in correct format',
Recipe_Validate_EmailAddress::EMAILISEMPTY =>
'Please enter email address'
)));
}
class Recipe_Validate_EmailAddress extends Zend_Validate_Abstract
{
const INVALID = 'notvalid';
const EMAILISEMPTY = 'isempty';
protected $_messageTemplates = array(
self::INVALID => "Email is in invalid format",
self::EMAILISEMPTY => "You have to fill email field"
);
public function isValid($value){
$response = parent::isValid($value);
if(!$response){
$this->_message = array(self::INVALID => "Please enter a valid email address");
}
return $response;
}
}
?>