Zend Form 验证器未触发
我正在尝试让我的 UniqueEmail 验证器正常工作,但似乎我的验证器从未被触发。
这是我的表单:
class EventManager_Form_User_Base extends SF_Form_Abstract{
public function init(){
$this->addElementPrefixPath(
'EventManager_Validate',
APPLICATION_PATH . '/modules/eventManager/models/validate',
'validate'
);
(...)
$this->addElement('text','usr_email', array(
'filters' => array('StringTrim', 'StringToLower'),
'valdators' => array(
array('StringLength',true,array(3,128)),
array('EmailAddress'),
array('UniqueEmail', false, array(new EventManager_Model_User())),
),
'required' => true,
'label' => 'email',
));
(...)
}
}
这是我的验证器
class EventManager_Validate_UniqueEmail extends Zend_Validate_Abstract{
const EMAIL_EXISTS = 'emailExists';
protected $_messageTemplates = array(
Self::EMAIL_EXISTS => 'Email "%value%" already exists in our system',
);
public function __constructs(EventManager_Model_User $model){
$this->_model = $model;
}
public function isValid($value,$context = null){
$this->_setValue($value);
$currentUser = isset($context['usr_id']) ? $this->_model->getUserById($context['user_id']) : null;
$user = $this->_model->getUserByEmail($value, $currentUser);
if(null === $user){
return true;
}
$this->_error(self::EMAIL_EXISTS);
return false;
}
}
当我在 isValid() 函数的第一行添加该行
var_dump($value); exit;
,然后运行我的表单时。然后代码就运行了,但似乎没有进入我的验证器。
我正在运行 zf 1.10.1 有什么想法/建议吗?
I am trying to get my UniqueEmail validator working but it seems that my validator is never triggered.
This is my form:
class EventManager_Form_User_Base extends SF_Form_Abstract{
public function init(){
$this->addElementPrefixPath(
'EventManager_Validate',
APPLICATION_PATH . '/modules/eventManager/models/validate',
'validate'
);
(...)
$this->addElement('text','usr_email', array(
'filters' => array('StringTrim', 'StringToLower'),
'valdators' => array(
array('StringLength',true,array(3,128)),
array('EmailAddress'),
array('UniqueEmail', false, array(new EventManager_Model_User())),
),
'required' => true,
'label' => 'email',
));
(...)
}
}
And here is my validator
class EventManager_Validate_UniqueEmail extends Zend_Validate_Abstract{
const EMAIL_EXISTS = 'emailExists';
protected $_messageTemplates = array(
Self::EMAIL_EXISTS => 'Email "%value%" already exists in our system',
);
public function __constructs(EventManager_Model_User $model){
$this->_model = $model;
}
public function isValid($value,$context = null){
$this->_setValue($value);
$currentUser = isset($context['usr_id']) ? $this->_model->getUserById($context['user_id']) : null;
$user = $this->_model->getUserByEmail($value, $currentUser);
if(null === $user){
return true;
}
$this->_error(self::EMAIL_EXISTS);
return false;
}
}
When I add the line
var_dump($value); exit;
in the first line of my isValid() function and then run my form. then the code just runs but seems not to get into my validator.
I am running zf 1.10.1 any idea's / suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
瓦尔达人 ->验证器。
valdators -> validators.
您看过 Zend_Validate_Db_RecordExists 验证器的示例吗?有趣的是,因为该示例检查数据库中的电子邮件,这与您的用例相同。
Have you seen the example of the Zend_Validate_Db_RecordExists validator? Funny since the example checks for emails in the DB which is the same as your use case.