我一直在使用Respect Validation进行表单验证
$app->post('/', function () use ($app) {
$validator = v::key('name', v::string()->notEmpty())
->key('email', v::email()->notEmpty())
->key('message', v::string()->notEmpty());
$errors = array();
try{
$validator->assert($_POST);
} catch (\InvalidArgumentException $e) {
$errors = $e->findMessages(array(
'notEmpty' => '{{name}} is required',
'email' => '{{name}} must be a valid email'
));
}
if ($validator->validate($_POST)) {
// do stuff
$app->redirect('/');
} else {
$app->render('index.php', array('field_errors' => array_values($errors)));
}
});
循环通过array_values($errors)
会给我:
"" is required
email must be a valid email
我需要类似的东西:
name is required
email must be a valid email
message is required
应该如何使用尊重验证来完成