Php Zend表单添加错误消息

Php Zend表单添加错误消息,php,zend-framework,forms,Php,Zend Framework,Forms,我确实用zend表格登记了表格 $password = new Zend_Form_Element_Password('password'); $password->setLabel($this->_translate->_("Password:")) ->setRequired(true) ->addValidator('stringLength', true, array(4, 32)); $confirmPassword = new Zend

我确实用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”字段添加一个
Zend\u Validate\u idential
验证器,使用
$this->\u request->getParam('password')
中的数据进行测试。我在扩展的Zend_表单上使用一个自定义方法来处理我所有表单中的post数据,这对您来说不是一个精确的解决方案,但也许我的“EditUser”表单中的代码可以为您指明正确的方向

从控制器:

// $form is a MW_Form_EditUser.

if ($this->_request->isPost() && $form->process($this->_request->getPost()))
{
   // successful form - redirect or whatever here
}
MW\u Form\u 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;
}

使用Zend_Validate_idential是Zend为您提供的一个很好的解决方案。我会给你另一个选择。jQuery。当您使用Zend_Validate_时,相同的表单将转到服务器,服务器将对其进行验证。如果密码不同,它将返回一条错误消息。如果使用jQuery,除非密码相同,否则表单将不会转到服务器。

覆盖在表单中有效

/**
     * Validate the form, check passwords.
     *
     * @param  array $data
     * @return boolean
     */
    public function isValid($data) {
        $valid = parent::isValid($data);
        if ($this->getValue('password') !== $this->getValue('password2')) {
            $valid = false;
            $this->password2->addError('Passwords don\'t match.');
        }

        return $valid;
    }

我认为,最好不要混淆:要命名函数isValid(),在其中创建Zend_Validate_idential,然后返回parent::isValid()。这个解决方案更自然,需要更少的代码。您需要进行服务器端验证,而客户端验证很好。没有服务器端的客户端是无用的。
//inside form

public function isValidPSW($data) {
        $valid = parent::isValid($data);
        if ($data['pswd'] !== $data['pswd2']) {
            $valid = false;
            $this->pswd->addError('Passwords don\'t match.');
        }
        return $valid;
    }