0

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
},
...
Boris Zagoruiko
  • 12,705
  • 15
  • 47
  • 79

1 Answers1

1

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();
}
Meeker
  • 5,979
  • 2
  • 20
  • 38