Reputation: 11532
I have a simple form that passes data to backbone, which in turn, submits requests to a tastypie REST API. The validation is done server side.
When a specific field has an error, how would I go about returning that error and handling it in backbone so it can be displayed alongside the field? Keeping in mind that backbone is expecting a model in return and not a bunch of errors (returning an array of errors with the associated fields sounds fine but I have a special backbone parse method that gets the objects out of the tastypie response - sending back random stuff will probably make it choke...?).
Confused as to why this isn't a common task to do.
Upvotes: 1
Views: 613
Reputation: 10627
Tastypie should be returning your validation errors with an http error-level code 4xx, not a success-level code 2xx.
Because of this, Backbone should be triggering the error callback, not the success callback.
Because the error callback is being triggered, Backbone doesn't invoke parse or anything else that happens on success.
So, you need to create a handler for tastypie errors, and pass that error handler function as the error callback.
From there, you can manually parse the body of the response and deal with the errors however you see fit for your app.
Upvotes: 1