我正在使用express validator。
如果字段为空,我想在validationResult中生成一个错误。
router.post('/register',[
check('name').isEmpty().withMessage('Name field cannot be empty.'),
check('email').isEmail().withMessage('Enter valid email address.'),
check('username').isEmpty().withMessage('Username cannot be empty.'),
check('password').isEmpty().matches('password2').withMessage('Password dont match.')
], function(req, res, next) {
const errors = validationResult(req);
console.log(errors.array());
if (!errors.isEmpty()) {
res.render('register2',{
errors: errors.mapped(),
name: req.body.name,
email: req.body.email,
username: req.body.username,
password: req.body.password,
password2: req.body.password2
});
}
});
当名称字段和用户名字段为空时,我想生成错误,但我只得到这些字段的错误:
[ { location: 'body',
param: 'email',
value: '',
msg: 'Enter valid email address.' },
{ location: 'body',
param: 'password',
value: '',
msg: 'Password dont match.' } ]