我正在使用快速验证器。
如果该字段为空,我想在 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.' } ]