In my client code, I check for the presence of an error attribute and react if necessary.
For example, I used the Collection.create function, which calls the add collection function if the request was successful. So, I redefined the Add Collection function to prevent the model from being added if it has the errors attribute and if it does not call the super method.
add: function(object, options) { if (_.isArray(object) || !object.get('errors')) { Backbone.Collection.prototype.add.call(this, object, options) } },
If my application returns a failure status code, it will work as well, as it will prevent successful callbacks from starting. But I did not like the idea of returning the 400 error just because the submission was invalid. This is something that I have never had to do in applications other than Backbone.js. ("Invertebrates"?)
None of the descriptions for the 4XX status codes seem to fit the concept of a failed check. 400 and 403 come nearer, but nevertheless come off, as if they were intended for other purposes. On the other hand, the user really doesn’t care which status code you return; may also be 404.
It really is a question of whether you want to write more code on the server side or on the client side. My application more or less ignores the test result and returns a record serialized in JSON, regardless of whether it was saved or not, so I decided to work from this angle.
source share