向Zend表单元素强制错误消息

时间:2011-02-15 14:15:29

标签: php zend-framework zend-form

    // Process the forms
    if (($this->getRequest()->isPost())
            && ($this->getRequest()->isXmlHttpRequest())) {

        // Initiate response
        $status = false;
        $msg = '';
        $zf = null;

        // Error test
        $form->getElement('no')->addError('This is the error message');            

        if ($form->isValid($this->getRequest()->getPost())) {

        // Everything is good
            $status = true;

        } else {

        // Get the error messages
            $zf = $form->getMessages();
        }

        // Setup the response
        $result = json_encode(array('status' => $status,
                                    'msg' => $msg,
                                    'zf' => $zf));

        $this->_helper->viewRenderer->setNoRender();
        $this->_helper->layout()->disableLayout();

        $this->getResponse()->setHeader('Content-Type', 'application/json');
        $this->getResponse()->setBody($result);

        return;

    } else {
        // Populate the form
    }

正如您所看到的,我已使用$form->getElement('no')->addError('This is the error message');强制表单元素出错,但$form->getMessages();仍会返回NULL。那么,我该怎么做才能强制选择表单元素上的错误呢?

2 个答案:

答案 0 :(得分:1)

我认为你得到了ErrorMessages()

$form->getErrorMessages()

答案 1 :(得分:0)

我已针对此问题打开了错误报告:http://framework.zend.com/issues/browse/ZF-11088。如果有任何新的进展,我会更新这个问题。