$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 からエラー メッセージを取得する正しい方法ですか、それとも他に何かありますか?