Zend表单添加错误消息

时间:2009-11-07 19:30:03

标签: 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_Form_Element_Password('confirmpassword');
$confirmPassword->setLabel($this->_translate->_("Confirm Password:"))
                        ->setRequired(true);

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

4 个答案:

答案 0 :(得分:5)

在表单中覆盖isValid

/**
     * 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;
    }

答案 1 :(得分:2)

这个概念基本上归结为在'confirmmpassword'字段中添加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;
}

答案 2 :(得分:1)

//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;
    }

答案 3 :(得分:-1)

Zend使用Zend_Validate_Identical是一个很好的解决方案。我会再给你一个选择。 jQuery的。当您使用Zend_Validate_Identical表单时,将转到服务器,服务器将验证它。如果密码不相同,则会返回错误消息。如果你使用jQuery表单将不会去服务器,除非密码是相同的。