Get error messages from Zend_Form and response as json
Asked Answered
S

1

14

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?

<?

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())

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?

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

Saunderson answered 31/7, 2012 at 11:33 Comment(3)
Have you tried getMessages()? I think this is the method you'd like to use to get human-friendly error messages.Stegosaur
@bububaba, yes I've tried, but it returns empty array, but $form->getErrors() returns array with validation codes (like isEmpty) (see above).Saunderson
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. getErrors returns codes, getErrorMessages returns registered custom error messages (seems probable you have none), while getMessages returns the actual human-friendly error messages. I'm posting this as an answer, do check it out :)Stegosaur
S
19

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

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.

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

Stegosaur answered 31/7, 2012 at 13:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.