2011-02-15 43 views
0
// 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。那么,我应该怎么做才能在选定的表单元素上强制执行错误?强制错误消息Zend的表单元素

回答

1

我想你已经得到了得到了ErrorMessages()

$form->getErrorMessages() 
+0

谢谢,但它不工作,我没有从`$ form-> getErrorMessages()`得到任何东西。我试图输出到Frirebug,我得到了空阵列。 – amree 2011-02-18 12:25:52