2009-11-07 84 views
2

我没有注册与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在控制器形成英寸如果密码和confirmpassword不匹配,则在confirmpassword文本框下添加错误消息。我怎样做?

回答

2

这个概念基本上归结为将Zend_Validate_Identical验证程序添加到'confirmpassword'字段,使用$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

我认为,最好不要是混乱:命名功能的isValid(),在其中创建Zend_Validate_Identical,就回到父母:: isValid()的。此解决方案更自然,需要更少的代码 – 2009-11-08 16:47:40

-1

使用Zend_Validate_Identical是由Zend公司为你一个很好的解决方案。我会给你另一个选择。 jQuery的。当你使用Zend_Validate_Identical表单时,会去服务器,服务器会验证它。如果密码不同,它会返回一条错误信息。如果你使用jQuery,表单将不会去服务器,除非密码相同。

+4

您必须具有服务器端验证,而客户端验证非常好。没有服务器端的客户端是没用的。 – smack0007 2009-11-08 18:27:10

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