Boris Zagoruiko
Boris Zagoruiko

Reputation: 13164

One model attribute equal to the other by default in sail.js model

Is there a way to set one attribute equal to the other in sails.js model before it is not updated? Something like:

username: {
    type: 'string',
    required: true
},

fullname: {
    type: 'string',
    defaultsTo: this.username
},
...

Upvotes: 0

Views: 132

Answers (1)

Meeker
Meeker

Reputation: 5979

Yes you can do this using a afterValidate callback. I reccomend afterValidate() because it will have checked for the already required username.

attributes : {
    username: {type:'string',required:true},
    fullname: {type:'string'}
},
afterValidate: function(values,next){
    if(!values.fullname) values.fullname = values.username;
    return next();
}

Upvotes: 1

Related Questions