Zend 2形成验证密码和密码重新输入

时间:2015-07-12 04:23:28

标签: zend-framework2

如果我只填写密码或只填写密码重新输入,那么它会通过验证。 如果我填写密码和密码重新输入并且它们不同,则它不会通过验证。

我做错了什么?!

    $inputFilter->add(array(
    'name' => 'password',
    'required' => true,
    'filters' => array(
        array('name' => 'StripTags'),
        array('name' => 'StringTrim'),
    ),
    'validators' => array(
        array(
        'name' => 'StringLength',
        'options' => array(
            'encoding' => 'UTF-8',
            'min' => 6,
            'max' => 128,
        ),
        ),
    ),
    ));

    $inputFilter->add(array(
    'name' => 'retype-password',
    'required' => true,
    'filters' => array(
        array('name' => 'StripTags'),
        array('name' => 'StringTrim'),
    ),
    'validators' => array(
        array(
        'name' => 'StringLength',
        'options' => array(
            'encoding' => 'UTF-8',
            'min' => 6,
            'max' => 128
        ),
        ),
        array(
        'name' => 'Identical',
        'options' => array(
            'token' => 'password'
        )
        )),
    ));

形式:

$form = $this->form;
$form->setAttribute('action', $this->url(
    'users_update',
    array(
    'action' => 'edit',
    'id'     => $this->id,
    )
));


$form->prepare();
echo $this->form()->openTag($form);
echo $this->formHidden($form->get('id'));
echo $this->formRow($form->get('username'));
echo $this->formRow($form->get('email'));
echo $this->formRow($form->get('password'));
echo $this->formRow($form->get('retype-password'));
echo '<br />';
echo $this->formSubmit($form->get('submit'));
echo '<br />';echo '<br />';
echo $this->form()->closeTag();

更新方法:

public function updateAction() {

$form = new UsersForm();

$id = (int) $this->params()->fromRoute('id', 0);
if (!$id) {
    return $this->redirect()->toRoute('admin');
}   

$user = $this->getUsersTable()->getUser( $id );

$request = $this->getRequest();
if ( $request->isPost()) {

    if( $request->getPost('password') == '' || $request->getPost('retype-password') == '' ){
    $request->getPost()->set('password'     , $user->password);
    $request->getPost()->set('retype-password'  , $user->password);
    }

    $users = new Users();
    $form->setInputFilter($users->getInputFilter());
    $form->setData($request->getPost());

    $form->isValid();

    if ($form->isValid()) {

    $users->exchangeArray($form->getData());
    $this->getUsersTable()->saveUser($users , $id);

    return $this->redirect()->toRoute('users_list');
    }
}



$form->bind($user);
$form->get('submit')->setValue('update');
return array(
    'id'    => $id,
    'form'  => $form
);
}

1 个答案:

答案 0 :(得分:2)

所以你有它。这件作品

    if ($request->getPost('password') == '' || $request->getPost('retype-password') == '' )
    {
        $request->getPost()->set('password'     , $user->password);
        $request->getPost()->set('retype-password'  , $user->password);
    }

导致症状。你应该有&amp;&amp;而不是||。