Zend_Dojo 表单密码文本框验证不起作用
Posted
技术标签:
【中文标题】Zend_Dojo 表单密码文本框验证不起作用【英文标题】:Zend_Dojo_Form passwordTextBox validator don't work 【发布时间】:2012-10-19 11:36:18 【问题描述】:我是zf的新手,最近我在用zend_dojo_form做一个注册表单,有两个passwordtextbox元素,你知道,一个是输入密码,另一个是确认前一个,然后我使用验证器'token' 但失败了,这是我的代码的一部分。
$this->addElement('PasswordTextBox','password',array(
'label'=>'password:',
'required'=>true,
'trim'=>true,
'regExp'=>'^[a-z0-9]6,18$',
'invalidMessage'=>'password should be 6-18',
'Decorators' => array(
'DijitElement',
'Errors',
array(array('data'=>'htmlTag'),array('tag'=>'td','align'=>'left')),
array('Label',array('tag'=>'td')),
array(array('row'=>'HtmlTag'),array('tag'=>'tr','align'=>'right'))
)
)
);
//$this->addElement($password1);
$this->addElement('PasswordTextBox','password2',array(
'label'=>'confirm password:',
'required'=>true,
'trim'=>true,
//'regExp'=>'^[a-z0-9]6,18$',
'validators'=>array(array('identical',false,array('token'=>'password'))),
'invalidMessage'=>'the password you enter not the same',
'Decorators' => array(
'DijitElement',
'Errors',
array(array('data'=>'HtmlTag'),array('tag'=>'td','align'=>'left')),
array('Label',array('tag'=>'td')),
array(array('row'=>'HtmlTag'),array('tag'=>'tr','align'=>'right'))
)
)
);
【问题讨论】:
【参考方案1】:这是一个应该可以正常工作的示例:
class Application_Form_Register extends Zend_Dojo_Form
public function init()
$this
->setOptions(array('name' => 'registerForm'))
// username
->addElement('TextBox', 'first_name', array(
'filters' => array('StringTrim', 'StringToLower'),
'id' => 'name_register',
'required' => true,
'label' => 'Fornavn(e):',
))
->addElement('TextBox', 'last_name', array(
'filters' => array('StringTrim', 'StringToLower'),
'id' => 'surname_register',
'label' => 'Efternavn:',
))
->addElement('ValidationTextBox', 'email', array(
'filters' => array('StringTrim', 'StringToLower'),
'validators' => array(
'EmailAddress',
array('StringLength', false, array(3, 60)),
),
'regExp' => "^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]1,3\.[0-9]1,3\.[0-9]1,3\.[0-9]1,3\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]2,))$",
'id' => 'email_register',
'required' => true,
'label' => 'E-mail:',
))
//password
->addElement('PasswordTextBox', 'password', array(
'filters' => array('StringTrim'),
'validators' => array(
'Alnum',
array('StringLength', false, array(6, 20)),
),
'regExp' => "^([a-zA-Z0-9_\-!-=])4,32$",
'tooltipPosition' => 'above',
'invalidMessage' => 'Krav til password: mellem 4 og 32 i længde og kun karakterer som tal, bogstaver eller blandt [ _-!"#%&/()=] er tilladt',
'id' => 'password_register',
'onKeyDown' => 'arguments[0].keyCode == dojo.keys.ENTER && application.submitRegistration(dijit.byId(\'registerForm\'));',
'required' => true,
'label' => 'Password:',
))
->addElement('PasswordTextBox', 'password_confirm', array(
'filters' => array('StringTrim'),
'validators' => array(
'Alnum',
array('StringLength', false, array(6, 20)),
),
'id' => 'passwordconfirm_register1',
'onKeyDown' => 'arguments[0].keyCode == dojo.keys.ENTER && application.submitRegistration(dijit.byId(\'registerForm\'));',
'required' => true,
'label' => 'Gentag password:',
))
->addElement('PasswordTextBox', 'password_confirm', array(
'filters' => array('StringTrim'),
'validators' => array(
'Alnum',
array('StringLength', false, array(6, 20)),
),
'id' => 'passwordconfirm_register2',
'onKeyDown' => 'arguments[0].keyCode == dojo.keys.ENTER && application.submitRegistration(dijit.byId(\'registerForm\'));',
'required' => true,
'label' => 'Gentag password:',
))
->addElement('Hidden', 'action', array(
'id' => 'action_register',
'value' => 'register',
))
->addElement('Button', 'foo', array(
'id' => 'foo_register',
'onClick' => 'application.submitRegistration(dijit.byId(\'registerForm\'))',
'label' => 'Register'
)
);
$this->_decorate();
// renders nulled empty labels and adds classnames, related to element name
protected function _decorate()
$this->setDecorators(array(
'FormElements',
array('HtmlTag', array('tag' => 'div', 'class' => 'zend_form_contents')),
'DijitForm'
));
foreach ($this->getElements() as $el)
$el->addDecorator('HtmlTag', array('tag' => 'div', 'class' => $el->getName() . '-wrap'));
if ($el->helper == 'Button')
continue;
else if (strlen(trim($el->getLabel())) == 0)
$el->addDecorator('Label', array('tag' => null));
else
$el->addDecorator('Label', array('tag' => 'div', 'class' => $el->getName() . '-label'));
关注两个密码字段——就客户端验证的原因而言,让我们过滤掉 Zend_Form_Element 属性:
->addElement('PasswordTextBox', 'password_2_', array(
// strictly serverside, only accepts alphanumerical pwds lengths 6 through 20
'validators' => array(
'Alnum',
array('StringLength', false, array(6, 20)),
),
// widget id
'id' => 'passwordconfirm_register1',
// both client-/server-side - triggers 'emptyMessage' validation
'required' => true,
// runs with the form.validate() and events onblur, oninit, onkeypress
// if ! dijit.get("value").match(dijit.regExp) show 'invalidMessage'
// this example matches the serverside reqs
'regExp' => "^([a-zA-Z0-9_\-!-=])6,32$",
// shown with form.validate(), dijit.validate() and input.blur() if regExp is not a match against value
'invalidMessage' => 'Password musts: 6 to 32 chars and only numbers, letters or amongst [ _-!"#%&/()=]',
))
还有第二个 pwd 字段,属性相同,但验证器不同
->addElement('PasswordTextBox', 'password_2_', array(
// strictly serverside, only accepts alphanumerical pwds lengths 6 through 20
'validators' => array(
array('identical',false,array('token'=>'password'))
),
// widget id
'id' => 'passwordconfirm_register2',
'required' => true,
// in theory, we would define 'validator' in programmatic construct of
// the validationtextbox. but this will not work with the way Zend handles dojo
// 'validator' => 'function(value, constraints) return "BAD PRACTICE" '
));
// add javascript code to run onload and to extend the 'register2' properly
$this->getElement()
->getView()
->headScript()
->appendFile($baseUrl . '/js/Validator.js');
到目前为止,在 passwordconfirm_register2 小部件中仅验证了客户端的“必需”。让我们修正一下标记工厂对上述行中的“不良做法”所做的事情。删除该行并添加 javascript 代码,其中包含:
// file: baseUrl/js/Validator.js
dojo.addOnLoad(function()
dijit.byId('passwordconfirm_register2').validate = function(value, constraints)
if(dijit.byId('passwordconfirm_register1').get("value") != value)
this.invalidMessage = "Passwords are not identical";
return false;
return true
);
);
【讨论】:
感谢您的回复,您的代码非常好。但也许我没有把我的问题说清楚,实际上,我的问题是关于密码确认。我使用zend_form be4,它通过使用'相同'验证器工作正常(如:array('identical',false,array('token'=>'password')),然后我切换到zend_dojo,它不去和我想的一样,有什么帮助吗? aha.. 那么问题是,您没有区分服务器端和客户端验证。 dijit.form.ValidationTextbox 没有“相同”参数。我们在表单元素中定义的“验证器”是 100% 的服务器端验证器;仅在回发例程中使用。我将在 dojo 世界中指定一个“链接”表单域 似乎添加外部 .js 文件对我来说相当困难,我尝试按照您的帖子的方式:$this->getElement() ->getView() ->headScript() -> appendFile($baseUrl . '/js/Validator.js');但我仍然无法正确处理,我使用 IE 开发工具,在 标签中看不到 .js 的引用 我相当肯定,'headScript' viewhelper 将这些参数带到 appendFile。但是,它可能会查找文件本身,如果不存在 - 在生成的代码中省略脚本标记。尝试插入完整的相对 url('/' 前缀)。$baseUrl
是必须设置的 :)以上是关于Zend_Dojo 表单密码文本框验证不起作用的主要内容,如果未能解决你的问题,请参考以下文章
选择框在 Internet Explorer 中不起作用/消失