how to validate password & password_conifrm ?

1,539 views
Skip to first unread message

Jxck

unread,
Jan 22, 2012, 4:34:06 AM1/22/12
to mongoo...@googlegroups.com
Hi all.

I have a form which has password & password_confirm inputs.
I want to validate password === password_confirm in mongoose.
and save only one column 'password' (means Schema has 'password' only, no 'password_confirm')

but mongoose validator is only for one column(can't see password_confirm value in password validator).
alse I tried pre('save'), but cant get 'password_confirm' if schema dosen't has password_confirm column.
is there any way to validate this?

Jxck

Aaron Heckmann

unread,
Jan 27, 2012, 2:27:55 PM1/27/12
to mongoo...@googlegroups.com
Hi Jxck,

A method could work for you here. Something like

schema.methods.setPassword = function setPassword (pwd, confirm) {
   if (pwd === confirm) {
     this.password = pwd;
     return true;
   } else {
     this.invalidate('password', new Error('Password mismatch'));
     return false;
   }
}



--
http://mongoosejs.com
http://github.com/learnboost/mongoose
You received this message because you are subscribed to the Google
Groups "Mongoose Node.JS ORM" group.
To post to this group, send email to mongoo...@googlegroups.com
To unsubscribe from this group, send email to
mongoose-orm...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/mongoose-orm?hl=en



--
Aaron


Jxck

unread,
Jan 31, 2012, 5:28:18 AM1/31/12
to mongoo...@googlegroups.com
Thanks Aaron.

but how to get "Error('Password mismatch')" ?
I can't get this error in callback of user.save().

# It seems official docs dosen't has how to use invalidate() :(

Jxck

Aaron Heckmann

unread,
Jan 31, 2012, 12:34:17 PM1/31/12
to mongoo...@googlegroups.com
crap, yeah its probably missing from the docs.

here's a working gist: https://gist.github.com/1711749


Jxck

--
http://mongoosejs.com
http://github.com/learnboost/mongoose
You received this message because you are subscribed to the Google
Groups "Mongoose Node.JS ORM" group.
To post to this group, send email to mongoo...@googlegroups.com
To unsubscribe from this group, send email to
mongoose-orm...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/mongoose-orm?hl=en



--
Aaron


Aaron Heckmann

unread,
Jan 31, 2012, 12:35:21 PM1/31/12
to mongoo...@googlegroups.com
its in the api docs but those pretty much suck as far as searchability goes

On Tue, Jan 31, 2012 at 5:28 AM, Jxck <block.rxc...@gmail.com> wrote:
# It seems official docs dosen't has how to use invalidate() :(



--
Aaron


Jxck

unread,
Feb 2, 2012, 12:46:51 AM2/2/12
to mongoo...@googlegroups.com
Hi Aaron.

Perfect !!

I solved my problem

Thanks a lot :)

Jxck
Reply all
Reply to author
Forward
0 new messages