12

在我的表单中,我试图验证用户是否两次都填写了相同的值(以确保他们没有犯错)。我认为这Zend_Validate_Identical就是用途,但我不太确定如何使用它。这是我到目前为止所得到的:

$this->addElement('password', 'password', array(
        'label'      => 'Password:',
        'required'   => true,
        'validators' => array(
            'Identical' => array(What do I put here?)
        )
    ));
$this->addElement('password', 'verifypassword', array(
        'label'      => 'Verify Password:',
        'required'   => true,
        'validators' => array(
            'Identical' => array(What do I put here?)
        )
    ));

我在这两个元素上都需要它吗?我在数组中放了什么?

4

7 回答 7

28

值得一提的是,1.10.5 版本中添加了对模型中两个相同表单字段进行比较的支持。我写了一个关于这个问题的简短教程,您可以通过下面的链接访问它,但最重要的是 Zend_Validate_Identical 验证器已被重构以接受表单字段名称作为输入。例如,要比较表单字段 pswd 和 confirm_pswd 的值,您需要将验证器附加到 confirm_pswd,如下所示:

$confirmPswd->addValidator('Identical', false, array('token' => 'pswd'));

奇迹般有效。

有关更完整的示例,请参阅使用 Zend 框架验证相同的密码。

于 2010-09-06T18:01:23.167 回答
5

我目前无法对其进行测试,但我认为这可能有效:

$this->addElement('password', 'password', array(
    'label'      => 'Password:',
    'required'   => true
));
$this->addElement('password', 'verifypassword', array(
    'label'      => 'Verify Password:',
    'required'   => true,
    'validators' => array(
        array('identical', true, array('password'))
    )
));
于 2009-10-27T03:40:33.450 回答
2

两天后我找到了正确的答案跟着我一步一步来:

第1步:

PasswordConfirmation.php使用此路径在项目的根目录中 创建文件:yourproject/My/Validate/PasswordConfirmation.php内容如下:

<?php 
require_once 'Zend/Validate/Abstract.php';
class My_Validate_PasswordConfirmation extends Zend_Validate_Abstract
{
    const NOT_MATCH = 'notMatch';

    protected $_messageTemplates = array(
        self::NOT_MATCH => 'Password confirmation does not match'
    );

    public function isValid($value, $context = null)
    {
        $value = (string) $value;
        $this->_setValue($value);

        if (is_array($context)) {
            if (isset($context['user_password']) 
               && ($value == $context['user_password']))
            {
                return true;
            }
        } 
        elseif (is_string($context) && ($value == $context)) {
            return true;
        }

        $this->_error(self::NOT_MATCH);
        return false;
    }
}
?>

第2步:

在表单中添加两个字段,如下所示:

//create the form elements user_password
$userPassword = $this->createElement('password', 'user_password');
$userPassword->setLabel('Password: ');
$userPassword->setRequired('true');
$this->addElement($userPassword);

//create the form elements user_password repeat
$userPasswordRepeat = $this->createElement('password', 'user_password_confirm');
$userPasswordRepeat->setLabel('Password repeat: ');
$userPasswordRepeat->setRequired('true');
$userPasswordRepeat->addPrefixPath('My_Validate', 'My/Validate', 'validate');
$userPasswordRepeat->addValidator('PasswordConfirmation', true, array('user_password'));
$this->addElement($userPasswordRepeat);

现在享受你的代码

于 2010-03-05T21:40:04.023 回答
1
class My_Validate_PasswordConfirmation extends Zend_Validate_Abstract
{
const NOT_MATCH = 'notMatch';

protected $_messageTemplates = array(
    self::NOT_MATCH => 'Password confirmation does not match'
);

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::NOT_MATCH);
    return false;
}
}

http://framework.zend.com/manual/en/zend.form.elements.html

于 2009-10-27T03:23:59.180 回答
1
$token = Zend_Controller_Front::getInstance()->getRequest()->getPost('password');
$confirmPassword->addValidator(new Zend_Validate_Identical(trim($token)))
                  ->addFilter(new Zend_Filter_StringTrim())
                  ->isRequired();   

在扩展 zend_form 的类中使用上述代码。

于 2009-10-28T10:55:11.757 回答
0

我能够让它与以下代码一起工作:

在我的表单中,我仅在第二个元素上添加了相同的验证器:

$this->addElement('text', 'email', array(
        'label'      => 'Email address:',
        'required'   => true,
        'filters'    => array('StringTrim'),
        'validators' => array('EmailAddress')
    ));

$this->addElement('text', 'verify_email', array(
        'label'      => 'Verify Email:',
        'required'   => true,
        'filters'    => array('StringTrim'),
        'validators' => array('EmailAddress', 'Identical')
    ));

在控制器中,就在调用之前isValid()

$validator = $form->getElement('verify_email')->getValidator('identical');
$validator->setToken($this->_request->getPost('email'));

我不知道是否有更优雅的方式来执行此操作,而无需将此代码添加到控制器。让我知道是否有更好的方法来做到这一点。

于 2009-10-27T05:33:18.747 回答
0

Zend Framework 1.10中,使用Zend FormZend Validate 验证两个字段是否相等所需的代码是:

    $form->addElement('PasswordTextBox',
                      'password',
                      array('label'      => 'Password')
                      );

    $form->addElement('PasswordTextBox',
                      'password_confirm',
                      array('label'      => 'Confirm password',
                            'validators' => array(array('Identical', false, 'password')),
                            )
                      );

您可以注意到,在password_confirm元素的验证器数组中,Identical验证器作为数组传递,该数组的语义是:i)验证器名称,ii)失败时中断链,iii)验证器选项 如您所见,这是可能的传递字段名称而不是检索值。

于 2010-08-28T10:57:04.840 回答