我试图找出如何使用 express-validator 和angularjs和html来显示验证错误,我的问题是我无法访问错误变量。请参阅下面的代码。
routes.js
router.post('/register', function(req, res){
var name = req.body.name;
var email = req.body.email;
var username = req.body.username;
var password = req.body.password;
var password2 = req.body.password2;
req.checkBody('name', 'Name is required').notEmpty();
req.checkBody('email', 'Email is required').notEmpty();
req.checkBody('email', 'Email not valid').isEmail();
req.checkBody('username', 'Username is required').notEmpty();
req.checkBody('password', 'Password is required').notEmpty();
req.checkBody('password2', 'Passwords does not match').equals(req.body.password);
var errors = req.validationErrors();
if(errors){
res.render('register.html', {
errors: errors
});
} else {
console.log('no errors');
}})
我在 server.js 中找到了似乎问题的内容。 这似乎导致快速路由和角度路由之间的问题,使快速路由失败,因此不能按需要传递数据。见下文。
app.use(function(req, res){
res.sendFile(__dirname + '/app/index.html');
});
app.use('/', routes);
app.use('/user', users);
如果我试试这个,请参阅下文。
app.use('/', routes);
app.use('/user', users);
app.use(function(req, res){
res.sendFile(__dirname + '/app/index.html');
});
这使得快速路由按照我想要的方式传递数据,但是我的角度路由似乎是错误的,一旦刷新页面 - 视图无法正确加载。
但是,我知道如何使用ejs或把手来显示它,但这不符合我的需要,应该有一种方法用角度存储错误并用html以某种方式显示它吗?
感谢任何帮助!
答案 0 :(得分:0)
您是否有理由不想使用angular自己的表单验证器?从ux的角度来看甚至可能更好,因为用户会立即知道他们的表单是否有效。它还允许您执行很酷的操作,例如禁用提交按钮,直到表单有效
prompt
在所需的输入中添加简单的ng-required可以满足您的大部分需求。设置输入类型=“电子邮件”将验证电子邮件的格式是否正确。
唯一有点棘手的可能是检查密码是否匹配,但即使可以通过属性指令实现。