From af5e8f5c72b5202f79d43415e553403a4195f7e1 Mon Sep 17 00:00:00 2001 From: Brad Dunbar Date: Fri, 22 Jun 2012 08:00:11 -0400 Subject: [PATCH] Fix #1433 - Do not save invalid models. --- backbone.js | 3 +++ test/model.js | 7 +++++++ 2 files changed, 10 insertions(+) diff --git a/backbone.js b/backbone.js index b08fa618..16bd9661 100644 --- a/backbone.js +++ b/backbone.js @@ -367,6 +367,9 @@ return false; } + // Do not persist invalid models. + if (!attrs && !this.isValid()) return false; + // After a successful server-side save, the client is (optionally) // updated with the server-side state. var model = this; diff --git a/test/model.js b/test/model.js index 9021b9d2..62661dff 100644 --- a/test/model.js +++ b/test/model.js @@ -847,4 +847,11 @@ $(document).ready(function() { .destroy({ success: function(){ ok(true); }}); }); + test("#1433 - Save: An invalid model cannot be persisted.", 1, function() { + var model = new Backbone.Model; + model.validate = function(){ return 'invalid'; }; + model.sync = function(){ ok(false); }; + strictEqual(model.save(), false); + }); + });