从Zend_Form获取错误消息,并以json形式响应

时间:2022-08-06 20:15:06

I'm trying to get error messages from Zend_Form and response as json. What is the best practice of getting Zend_Form errors and replying as json?

我正在尝试从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());

    }

}

I can't get errors via $form->getErrorMessages(), but errors are present if tested print_r($form->gerErrors())

我不能通过$form-> geterrormessage()获取错误,但是如果测试print_r($form->gerErrors(),则会出现错误。

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

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

    [foreign] => Array
        (
        )

    [login] => Array
        (
        )

)

So, my questions are:

所以,我的问题是:

a) How to get all error messages for form?

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

b) Is there any Json Wrapper for resposning ajax submitted forms? For example $jsonResponse->setErrorStatus()->addFormErrors($form)

b)对于重新生成ajax提交的表单是否有Json包装?例如$ jsonResponse - > setErrorStatus()- > addFormErrors($)

1 个解决方案

#1


18  

Have you tried getMessages? I think this is the method you'd like to use to get human-friendly error messages.

你有试过getMessages吗?我认为这是您希望使用的方法来获取人类友好的错误消息。

You wrote you've tried getErrorMessages and getErrors, but getMessages is a different beast altogether, that's why I'm asking whether you've tried it.

你写过你尝试过getErrorMessages和getErrors,但是getMessages完全不同,所以我问你是否尝试过。

getErrors returns codes, getErrorMessages returns registered custom error messages (seems probable you have none), while getMessages returns the actual human-friendly error messages.

getErrors返回代码,geterrormessage返回已注册的自定义错误消息(很可能您没有),而getmessage返回实际的人类友好的错误消息。

#1


18  

Have you tried getMessages? I think this is the method you'd like to use to get human-friendly error messages.

你有试过getMessages吗?我认为这是您希望使用的方法来获取人类友好的错误消息。

You wrote you've tried getErrorMessages and getErrors, but getMessages is a different beast altogether, that's why I'm asking whether you've tried it.

你写过你尝试过getErrorMessages和getErrors,但是getMessages完全不同,所以我问你是否尝试过。

getErrors returns codes, getErrorMessages returns registered custom error messages (seems probable you have none), while getMessages returns the actual human-friendly error messages.

getErrors返回代码,geterrormessage返回已注册的自定义错误消息(很可能您没有),而getmessage返回实际的人类友好的错误消息。