Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Call custom function that validates whole model instead of fields #234

Open
taivo opened this issue Jul 17, 2014 · 1 comment
Open

Call custom function that validates whole model instead of fields #234

taivo opened this issue Jul 17, 2014 · 1 comment

Comments

@taivo
Copy link

taivo commented Jul 17, 2014

Is there a way to call a function that validates the model as a whole instead of individual attributes. This would allow more flexible validation of relationship among attributes instead of just their values.

This also allows working with inherited model classes that already have a validate() method defined. For example, if you're working with a mobile backend like Parse.com, whose js API is based on Backbone but instead of Backbone.Model, you are required to use Parse.Object. This class has a method Parse.Object.validate that takes care of a few behind the scene data fields. I'm trying to figure out a way to call Parse.Object.validate either before or after the normal flow of backbone-validation

@alancasagrande
Copy link

Not exactly pretty but one way you can do that:

_.extend(MyModel.prototype, Backbone.Validation.mixin);

MyModel.prototype.validate = function () {
  var errors = Backbone.Validation.mixin.validate.apply(this, arguments);

  if (errors) { return errors; }

  // put you validation here
};

Hope it helps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants