chriso/node-validator — pull request #72 by orfaust (2012-03-28 02:05:05+00:00)
Title: get back the param used in express-validator

This commit is meant to work in conjunction with another pull request on express-validator that I'll make if you accept this one.
https://github.com/orfaust/express-validator/commit/dedfa03e1f41c558c64d2d786e67abaaa71ed4f5

These changes don't break the normal use of node-validator (at least they should not :)
The purpose is to let you get back the param used to check a field and make a hash of errors to display in a template:

var errors = {};
req.onValidationError(function(msg, param)
{
var error = {};
errors[param] = msg;
return this;
});

req.check('email', 'Email not valid').isEmail();
req.check('password', 'Password is empty').notEmpty();

if(errors)
{
console.log(errors);
return;
}

output in case of errors:

{ email: 'Email not valid', password, 'Password is empty' }

Then you'd use it in a template like:

- if(errors.email)
input.error(type="text", name="email", value="#{values.email}")
span.error= errors.email
- else
input.(type="text", name="email")