From b5d3b51c13d304b69414e9e57988579beb2fa711 Mon Sep 17 00:00:00 2001 From: Charley Bodkin Date: Tue, 7 Feb 2017 16:38:24 -0800 Subject: [PATCH 1/2] added schemaValidate option to form.commit() to allow for schema validation to be skipped if set to false. form.validate() can be called with {skipSchemaValidate:true} to bypass schema validation as well. --- src/form.js | 17 ++++++++++------- test/form.js | 19 +++++++++++++++++++ 2 files changed, 29 insertions(+), 7 deletions(-) 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(); From d0de00f86d0767ade817b16c81010d2b491c3cba Mon Sep 17 00:00:00 2001 From: Charley Bodkin Date: Tue, 7 Feb 2017 16:44:39 -0800 Subject: [PATCH 2/2] added documentation on form.commit({ schemaValidate: false }) --- README.md | 2 ++ 1 file changed, 2 insertions(+) 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