1

While using sails validation I need some fields only to get validated while updating and not during creation. The scenario is that when the user is getting creating the i am just taking username and password and later I ask for all the remaining data for the user and I need to apply some validation rules only during updation and not creation.

I know I can do some manual validation using lifecycle callback beforeUpdate but then I wont be using sails validation which I think is not the proper way.

How can tell the model that these rules need to be applied only during updation?

Vyper
  • 573
  • 1
  • 6
  • 17

2 Answers2

1

what rules specifically? One thing you can do is give the rules a true/false check to see if the record has as an Id. If it does, then its an update, if it does not, then its a create.

favorite_color : {
    required : function(){
       return 'id' in this // ID should be your primary key field. 
    }
}

I will say it would be nice if you could specify when validations occur, but currently you can not.

Meeker
  • 5,979
  • 2
  • 20
  • 38
  • Thanks Meeker! This can be one work around but what I am doing right now is using the beforeUpdate callback for this purpose and using the validator.js directly which sails is also using for validation purpose ! – Vyper Dec 11 '14 at 11:50
0

You may split DB entity into two separate entities and use two models. First model will require username and password and second model will require other fields.

Zuker
  • 1,065
  • 1
  • 8
  • 12