如何在字段上设置相同的验证器而不使其成为Zendframework 1中的必填字段+允许检查空值。
这是代码
$this->addElement('password', 'password', array(
'label' => 'password',
'required' => false
),
));
$this->addElement('password', 'confirm', array(
'label' => 'confirm',
'required' => false,
'validators' => array(
array(
'validator' => 'Identical',
'options' => array(
'token' => 'password'
)
)
)
));
如果确认元素设置为' required' => false,则相同的验证器在值为空时不起作用。
答案 0 :(得分:0)
我最后编写了自己的验证码来确认密码:
class MyLib_Validate_PasswordConfirmation extends Zend_Validate_Abstract
{
const PASSWORD_MISMATACH = 'passwordMismatch';
protected $_messageTemplates = array(
self::PASSWORD_MISMATCH => 'This value does not match the confirmation value.'
);
/**
* Requires that 'password_confirm' be set in $context.
*/
public function isValid($value, $context = null)
{
$value = (string) $value;
$this->_setValue($value);
if (is_array($context)) {
if (isset($context['password_confirm'])
&& ($value == $context['password_confirm']))
{
return true;
}
} elseif (is_string($context) && ($value == $context)) {
return true;
}
$this->_error(self::PASSWORD_MISMATCH);
return false;
}
}
答案 1 :(得分:0)
您不能在required
之后使用相同的验证码,因为在isValid()
的{{1}}方法中有以下内容:
Zend_Form_Element
和 if ((('' === $value) || (null === $value))
&& !$this->isRequired()
&& $this->getAllowEmpty()
) {
return true;
}
是Zend_Form_Element_Password
的一个实例。
我认为你有三种可能性:
Zend_Form_Element
元素中添加required
选项,但您需要具体的消息confirm
元素中将allowEmpty
选项添加为false,如下所示:confirm
代替'allowEmpty' => false,
'required' => false,
。否则,您可以在网上找到更多信息。 :)