Cuong Pham
Cuong Pham

Reputation: 25

How to run beforeValidate only for update action in Sails framework?

I want to know current action when trigger beforeValidate in Sails framework, please review my code:

module.exports = {
    attributes: {
        ...
        log: 'string'
    },
    beforeValidate: function(values, cb) {

        // I want to know the action is create new record or update current record
        // If action is update, how to get primary key of record will be updated

        /*
        if (isCreateNewRecord) {
            values.log = 'New created...'; // for example
        } else {
            values.log = 'Update record ID: ' + recordID;
        }
        */

        cb();
    }
}

Thanks so much for help!

Upvotes: 1

Views: 517

Answers (1)

Yann Bertrand
Yann Bertrand

Reputation: 3114

Well, you're not supposed to do that... And this is why we have beforeCreate and beforeUpdate!

beforeCreate: function (values, cb) {
  values.log = 'New created...';

  return cb();
},

beforeUpdate: function (valuesToUpdate, cb) {
  valuesToUpdate.log = 'Update record ID: ' + valuesToUpdate.id;

  return cb();
}

Check out the docs.

Upvotes: 2

Related Questions