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) ajax で送信されたフォームを応答するための Json ラッパーはありますか? 例えば$jsonResponse->setErrorStatus()->addFormErrors($form)

4

1 に答える 1

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.

于 2012-07-31T13:03:59.337 に答える