Logic of beforeValidate() in sails.js -
i new sails.js, , after works find beforevalidate()
hard use.
i have model user
2 attributes nickname
, age
. both attributes marked required
sails check validation. when creating user, if client not specify nickname, assign random nickname it, following:
beforevalidate: function(user, next){ if (!user.nickname) user.nickname = randomstring(5); next(); }
this assigning should applied before sails validates required
tag of nickname
, beforevalidate()
seems reasonable place it. however, when want update user's age like:
user.update(user.id, {age: 40});
it trigger beforevalidate()
and, sadly, new nickname assigned , replace origin one, since parameter user
in beforevalidate()
{age: 40}
.
what expect parameter user
in beforevalidate(user, callback)
should updated user, combination of both changed , unchanged attributes (e.g. user old id, old nickname , new age), instead of changed attributes specify in user.update()
(e.g. {age: 40}
).
do misunderstand something?
while calling create()
values required add new record passed beforevalidate
. whereas while calling update()
values getting updated passed beforevalidate
. reason getting user.nickname
empty value.
refer docs @ http://sailsjs.org/documentation/concepts/models-and-orm/lifecycle-callbacks.
callbacks on create
beforevalidate: fn(values, cb)
callbacks on update
beforevalidate: fn(valuestoupdate, cb)
Comments
Post a Comment