2015-07-12 74 views
0

如果我只填寫密碼或只輸入密碼,那麼它會通過驗證。 如果我填寫密碼和密碼重新輸入並且它們不同,那麼它不會通過驗證。Zend 2 form validaton password and password-retype

我做錯了什麼?

$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 
); 
} 
+0

適合我。你確定你的表格正確嗎? – akond

+0

@akond我發佈了表單和更新方法。 – Bas

回答

2

所以你有它。這件

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

導致症狀。您應該有& &而不是||。

+0

Oeps!謝謝! – Bas