$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 框架和 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 获取错误消息的正确方法还是还有其他什么?