Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
313 views
in Technique[技术] by (71.8m points)

php - Zend_Form: how to check 2 fields are identical

I have created a form to add a user to a database and make user available for login.

Now I have two password fields (the second is for validation of the first). How can I add a validator for this kind of validation to zend_form?

This is my code for the two password fields:

    $password = new Zend_Form_Element_Password('password', array(
        'validators'=> array(
            'Alnum',
            array('StringLength', array(6,20))
            ),
        'filters'   => array('StringTrim'),
        'label'     => 'Wachtwoord:'
        ));

    $password->addFilter(new Ivo_Filters_Sha1Filter());

    $password2 = new Zend_Form_Element_Password('password', array(
        'validators'=> array(
            'Alnum',
            array('StringLength', array(6,20))
            ),
        'filters'   => array('StringTrim'),
        'required'  => true,
        'label'     => 'Wachtwoord:'
        ));
    $password2->addFilter(new Ivo_Filters_Sha1Filter());
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The current version of Zend_Validate has this built in - while there are plenty of other answers, it seems that all require passing a value to Zend_Validate_Identical. While that may have been needed at one point, you can now pass the name of another element.

From the Zend_Validate section of the reference guide:

Zend_Validate_Identical supports also the comparison of form elements. This can be done by using the element's name as token. See the following example:

$form->addElement('password', 'elementOne');
$form->addElement('password', 'elementTwo', array(
    'validators' => array(
        array('identical', false, array('token' => 'elementOne'))
    )
));

By using the elements name from the first element as token for the second element, the validator validates if the second element is equal with the first element. In the case your user does not enter two identical values, you will get an validation error.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...