I have registration form and posting the values to register function where it will validate form values with node-validator.
But, when I tried with node-validator, it keeps getting error as TypeError: Object # has no method 'onValidationError'. The validator already installed in my app.
In app.js
app.post('/register', test.register);
In test.js
var check = require('validator').check,
sanitize = require('validator').sanitize;
exports.register = function(req, res){
var errors = [];
req.onValidationError(function(msg) {
//res.render('signup', { error: msg });
errors.push(msg);
});
req.check(req.body.email, 'Please enter a valid email').len(6,64).isEmail();
req.check(req.body.username, "Username can't be empty!").isNull();
if (errors.length)
return res.render(
'register', { regErr: errors.join('\n') }
);
}
How to correct this error?
You are using node-validator . You should be using express-validator for onValidationError
From node-validator page you can see it:
If you are using the express.js framework you can use the express-validator middleware to seamlessly integrate node-validator.
Example http://localhost:8080/?zip=12345&foo=1&textarea=large_string
get('/', function (req, res) {
req.onValidationError(function (msg) {
//Redirect the user with error 'msg'
});
Additional Note:
The order of app.use is also important. Try to move app.use(expressValidator); up of app.use(app.router);. You can view the order of app.use in https://github.com/justin-john/node-user-management/blob/master/app.js#L17-L18.