Form validation #4

PaulCampbell opened this Issue Mar 1, 2012 · 2 comments

3 participants


I'm looking for a way to do form validation. My situation is as follows (I'm pretty new to node dev, so I may not be doing things entirely right, but here we go...)

I have a 'create' route on a controller as follows:

AccountsController.create = function() {
  var account = new Account(); = this.param('email');
  account.password = this.param('password');

  var self = this;
  account.register(function (err) {
    // There was a validation problem!
    return self.redirect(self.urlFor({  action: 'new' }));
    // we're good - the model was created... crack on.
    return self.redirect(self.urlFor({ action: 'login' }));

Account is a mongo model - The register method should probably be static, but I think the general gist is there...

If register returns validation problems (from mongo - lack of required fields etc) they are contained in the err argument passed to me fail callback.

I think I would like to register the problems on the response then redirect and have them available in the view. eg:

account.register(function (err) {
    return self.redirect(self.urlFor({  action: 'new' }));
    return self.redirect(self.urlFor({ action: 'login' }));

addFormState method adds the errors object to some property available in the view -> in this case it is the mongo error - probably not so cool because I don't want to couple to mongo error messages... I think error object available on the view needs to be an array of something like:

{ formFieldName: 'email', errorMessage: 'email address required'}

Does this seem a reasonable approach?

if so I am up for having a punt at creating some code for this sort of thing, or helping out on an effort. As I said - new to node, so may not get it right, but I can have a go.

Do you think there are any other requirements form validation might need, or perhaps a better way of addressing this problem?




Hi Paul,

I'd recommend using a library like


Validation is out of scope of Locomotive. Choose whatever validation module you prefer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment