Zend_Form不显示调用addError的错误消息

时间:2012-01-23 19:39:37

标签: zend-framework zend-form

我正在实现updatePasswordAction,并且它没有显示当前密码无效的错误。我无法使用Zend_Validate类与提供的记录 - >密码一起使用,所以我现在只在我的控制器操作中验证,如果失败,那么我将错误消息添加到表单元素。这是在我运行$form->isValid之前。无论如何,它的工作。但是当验证失败时,它不会在元素上显示错误消息。任何帮助将不胜感激。

仅供参考:当我提交空白当前密码时,会显示验证

class Admin_Form_UserPassword extends Katana_Form
{
    public function init()
    {
    $element = $this->createElement('hidden', 'id');
    $this->addElement($element);

    $element = $this->createElement('password','password');
    $element->setLabel('Current Password:');
    $element->setRequired(true);        
    $this->addElement($element);

    $element = $this->createElement('password','new_password');     
    $element->setLabel('New Password:');
    $element->addValidator('StringLength', false, array(6,24));     
    $element->setRequired(true);
    $element->addValidator('NotEmpty');     
    $this->addElement($element);

    $element = $this->createElement('password','new_password_confirm');
    $element->setLabel('Confirm:');
    $element->addValidator('StringLength', false, array(6,24));
    $element->addValidator('IdenticalField', false, array('new_password', 'Confirm Password'));
    $element->setRequired(true);
    $this->addElement($element);

    $this->addElement('submit', 'submit', array('label' => 'Submit'));
}

}

public function updatePasswordAction()
{
    $resourceModel  = new Core_Model_Resource_User();       
    $form           = new Admin_Form_UserPassword();
    $form->setMethod(Katana_Form::METHOD_POST);
    $form->setAction($this->getActionUrl('update-password'));
    if($this->getRequest()->isPost()){
        $id             = $this->getRequest()->getParam('id');
        $record         = $resourceModel->find($id)->current();
        $currPassword   = $record->password;
        $typedPassword  = md5($this->getRequest()->getParam('password'));           
        if($currPassword !== $typedPassword){               
            $form->getElement('password')->addError('Current password is incorrect.');
        }
        if($form->isValid($_POST)){
            $data       = $form->getValues();
            $result     = $resourceModel->updatePassword($id, $data['new_password']);
            if($result){
                $this->redirectSimple('list');
            }
        }
    } else {
        $id     = $this->getRequest()->getParam('id');          
        $recordData = array(
            'id' => $id
        );
        $form->populate($recordData);           
    }
    $this->getView()->form = $form;     
}

2 个答案:

答案 0 :(得分:6)

向元素添加错误不会导致表单本身无效。

至少有两种方法可以解决这个问题:

if($currPassword !== $typedPassword){               
    $form->getElement('password')->addError('Current password is incorrect.');
    $form->markAsError();
}

// or

if ($form->isValid($_POST) && 0 == sizeof($form->getMessages()) {
    // form was valid, and no errors were set on elements
}

为了澄清,当你将错误添加到ELEMENT表单时,该元素附加了一个错误,但Zend_Form :: isValid只运行验证器并设置了相应的错误,它不检查你是否设置了特定元素的错误。

但是,您可以调用$form->getMessages()来获取附加到表单或其子元素的所有错误消息。如果这是0并且您已经验证了表单,则表示没有错误。如果您的表单已通过isValid,但您向元素添加了错误,则会包含您添加的错误消息。

答案 1 :(得分:0)

我这样做了。

控制器:

if ($trial->getKind() != 'debt' && $_POST['kind'] == 'debt')
{
    $editForm->getElement('kind')->markAsError();
}

if ($editForm->isValid($_POST)) { ... }

形式:

public function isValid($data)
{
    $valid = parent::isValid($data);

    if ($this->getElement('kind')->hasErrors()) {
        $this->getElement('kind')->addError($this->_translate->translate('You can\'t change trial kind to debt.'));
        $valid = false;
    }

    return $valid;
}

并且this comment帮助了我。