我确实用zend表格注册表格

$password = new Zend_Form_Element_Password('password');
$password->setLabel($this->_translate->_("Password:"))
    ->setRequired(true)
    ->addValidator('stringLength', true, array(4, 32));

$confirmPassword = new Zend_Form_Element_Password('confirmpassword');
$confirmPassword->setLabel($this->_translate->_("Confirm Password:"))
                        ->setRequired(true);


我在控制器中控制密码并确认密码。如果密码和Confirmpassword不匹配,则在Confirmpassword文本框下添加错误消息。我怎样做?

最佳答案

这个概念基本上归结为在Zend_Validate_Identical验证器,使用来自$this->_request->getParam('password')的数据进行测试。我在扩展的Zend_Form上使用自定义方法来处理所有表单中的帖子数据,这不是您的确切解决方案,但是也许我的“ EditUser”表单中的代码可以为您指明正确的方向。

从控制器:

// $form is a MW_Form_EditUser.

if ($this->_request->isPost() && $form->process($this->_request->getPost()))
{
   // successful form - redirect or whatever here
}


MW_Form_EditUser类中:

public function process(array $data)
{
 // gets a copy of the user we are editing
 $user = $this->getEditable();
 // checks to see if the user we are editing is ourself
 $isSelf = ($user->id == MW_Auth::getInstance()->getUser()->id);

 // if the new_pass field is non-empty, add validators for confirmation of password
 if (!empty($data['new_pass']))
 {
   $this->new_pass2->setAllowEmpty(false)->addValidator(
       new Zend_Validate_Identical($data['new_pass'])
     );
   if ($curpass = $this->current_password) $curpass->setAllowEmpty(false);
 }

 if ($this->delete && !empty($data["delete"])) {
   $this->delete->setValue(true);
   $user->delete();
   return true;
 }

 if ($this->isValid($data))
 {
   /// saves the data to the user
   $user->email = $this->email->getValue();
   $user->name = $this->name->getValue();
   if ($password = $this->new_pass->getValue()) $user->password = $password;
   if (!$isSelf)
   {
     if ($this->super->getValue()) {
       $user->setGroups(array(MW_Auth_Group_Super::getInstance()));
     } else {
       $user->setGroups(array(MW_Auth_Group_User::getInstance()));
     }
   }
   $user->save();
   return true;
 }
 return false;
}

10-02 02:59