diff --git a/README.md b/README.md index 351e42a8..a94144ff 100644 --- a/README.md +++ b/README.md @@ -719,6 +719,8 @@ built-in Backbone model validation. Backbone Forms will run both when `form.validate()` is called. Calling `form.commit()` will run schema level validation by default, and can also run model validation if `{ validate: true }` is passed. +Calling `form.commit({ schemaValidate: false })` will skip schema level +validation and allow you to write dirty form data to the model. Use with care. ###Schema validation diff --git a/src/form.js b/src/form.js index 814f9d0d..d1a9b970 100644 --- a/src/form.js +++ b/src/form.js @@ -261,12 +261,14 @@ var Form = Backbone.View.extend({ options = options || {}; //Collect errors from schema validation - _.each(fields, function(field) { - var error = field.validate(); - if (error) { - errors[field.key] = error; - } - }); + if(!options.skipSchemaValidate) { + _.each(fields, function(field) { + var error = field.validate(); + if (error) { + errors[field.key] = error; + } + }); + } //Get errors from default Backbone model validator if (!options.skipModelValidate && model && model.validate) { @@ -317,7 +319,8 @@ var Form = Backbone.View.extend({ options = options || {}; var validateOptions = { - skipModelValidate: !options.validate + skipModelValidate: !options.validate, + skipSchemaValidate: options.schemaValidate === false }; var errors = this.validate(validateOptions); diff --git a/test/form.js b/test/form.js index 0dea90df..f9efadb4 100644 --- a/test/form.js +++ b/test/form.js @@ -826,6 +826,25 @@ test('returns validation errors', function() { same(err.foo, 'bar'); }); +test('does not return schema validation errors if commit({ schemaValidate: false })', function() { + var model = new Backbone.Model; + + model.validate = function() { + return 'FOO'; + }; + + var form = new Form({ + model: model, + schema: { + title: {validators: ['required']} + } + }); + + var err = form.commit({schemaValidate: false}); + + same(err, undefined); +}); + test('does not return model validation errors by default', function() { var model = new Backbone.Model();