如何验证"字段"在node.js上形式强大吗?我想确保所有字段都由用户填写。在express中我们有req.validationErrors()
form.parse(req, function(err, fields, files)
提前致谢
答案 0 :(得分:1)
您可以抛出错误,然后使用form.on('error', fn)
抓住它
这是一个简单的例子:
// Create a Route (/upload) to handle the form submission.
app.post('/upload', function(req, res, next) {
var requiredFields = ['name', 'email'];
// Whenever a field / value pair has been received.
form.on('field', function(name, value) {
if (requiredFields.indexOf(name) > -1 && !value) {
// field is required and its value is empty
form._error('Required field is empty!');
return;
}
});
// Send error message back to client.
form.on('error', function (message) {
res.end(message);
});
});