Skip to content

Commit

Permalink
Merge pull request jashkenas#1440 from braddunbar/save-invalid
Browse files Browse the repository at this point in the history
Fix jashkenas#1433 - Do not save invalid models.
  • Loading branch information
jashkenas committed Jun 22, 2012
2 parents 10b3b38 + af5e8f5 commit 17189f3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
3 changes: 3 additions & 0 deletions backbone.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 7 additions & 0 deletions test/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

});

0 comments on commit 17189f3

Please sign in to comment.