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

发布于 2024-12-28 13:19:53 字数 2724 浏览 3 评论 0原文

我正在实现 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;     
}

I am implementing an updatePasswordAction and its not displaying an error with an invalid current password. I could not implement this with a Zend_Validate class to use with a supplied record->password, so i just validated for now in my controller action and if failed then i add the error message to the form element. this is just before i run $form->isValid. In any case, its working. but when the validation fails, its not displaying the error message on this on the element. any help would be greatly appreciated.

FYI: When I submit a blank current password, it shows the validation

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

七度光 2025-01-04 13:19:53

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

我至少使用 2 种方法来解决这个问题:

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,但您向元素添加了错误,则它将包含您添加的错误消息。

Adding an error to the element doesn't cause the form itself to then be invalid.

There are at least 2 methods I use to get around this:

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
}

To clarify, when you add the error to the form ELEMENT, there is an error attached to that element, but Zend_Form::isValid only runs the validators and sets appropriate errors, it doesn't check to see if you had set an error on a particular element.

You can however call $form->getMessages() to get all the error messages attached to the form or its child elements. If this is 0 and you have validated your form, then it means there were no errors. If your form passed isValid but you added an error to an element, it will include the error message you added.

冷心人i 2025-01-04 13:19:53

我就是这样让它工作的。

控制器:

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

此评论帮助了我。

I made it work this way.

Controller:

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

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

Form:

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

And this comment helped me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文