14

我正在尝试从 Zend_Form 获取错误消息并作为 json 响应。获取 Zend_Form 错误并以 json 形式回复的最佳实践是什么?

<?

class SomeController extends Zend_Controller_Action {

    public function indexAction() {

        $form = new Application_SomeForm();
        if ($form->isValid( $this->getRequest()->getPost() )) {
            //do something here
        }       
        $this->_helper->json($form->getErrorMessages());

    }

}

我无法通过 获取错误$form->getErrorMessages(),但如果经过测试,则会出现错误print_r($form->gerErrors())

Array
(
    [email] => Array
        (
            [0] => isEmpty
        )

    [password] => Array
        (
            [0] => isEmpty
        )

    [foreign] => Array
        (
        )

    [login] => Array
        (
        )

)

所以,我的问题是:

a) 如何获取表单的所有错误消息?

b) 是否有任何 Json Wrapper 用于响应 ajax 提交的表单?例如$jsonResponse->setErrorStatus()->addFormErrors($form)

4

1 回答 1

19

你试过getMessages吗?我认为这是您想要用来获取人性化错误消息的方法。

你写了你已经尝试过getErrorMessagesgetErrorsgetMessages完全不同,这就是为什么我要问你是否尝试过。

getErrors返回代码,getErrorMessages返回注册的自定义错误消息(似乎你没有),同时getMessages返回实际的人性化错误消息。

于 2012-07-31T13:03:59.337 回答