2
$app->post('/', function () use ($app) {

    $email = new Input('email');
    $email->getValidatorChain()
          ->addValidator(new Validator\EmailAddress());

    $password = new Input('name');
    $password->getValidatorChain()
             ->addValidator(new Validator\StringLength(1));

    $inputFilter = new InputFilter();
    $inputFilter->add($email)
                ->add($password)
                ->setData($_POST);

    if ($inputFilter->isValid()) {

        // do stuff

        $app->redirect('/');

    } else {

        $field_errors = array();

        foreach ($inputFilter->getInvalidInput() as $field => $error) {
            foreach ($error->getMessages() as $message) {
                $field_errors[] = str_replace('Value', ucfirst($field), $message);
            }
        }

        $app->render('index.php', array('field_errors' => $field_errors));

    }
});

現在、Slim Framework と Zend InputFilter を使用して上記のコードを作成しています。ただし、エラーメッセージを取得したい。私は「値....」を取得し続けるので、以下のようになるようstr_replaceにそれらを実行しましたEmail is not a valid email:

        $field_errors = array();

        foreach ($inputFilter->getInvalidInput() as $field => $error) {
            foreach ($error->getMessages() as $message) {
                $field_errors[] = str_replace('Value', ucfirst($field), $message);
            }
        }

これは Zend InputFilter からエラー メッセージを取得する正しい方法ですか、それとも他に何かありますか?

4

1 に答える 1

5

キー付き配列を取得するには、 $inputFilter->getMessages() を呼び出すだけです。

array(
    'input' -> 'message', 
    'inputtwo' => 'anothermessage',
);

これは内部的に getInvalidInput() を使用するため、ネストされた foreach ループを使用する必要はありません。getMessages() を 1 回呼び出すだけで十分です。

于 2013-01-28T14:03:54.597 に答える