The Form’s save
method returns a Promise. When the response status is out of the 2xx range we can catch
and receive an object with status
and statusText
.
But sometimes this information is not enough. For example, I’m using API Platform and I can’t get information about what field is throwing a validation error in my back-end.
I guess that could be util to receive also the XMLHttpRequest object, as I show in the example.
var xhr = new XMLHttpRequest();
xhr.onload = function () {
if (xhr.status >= 200 && xhr.status < 300) {
resolve(xhr.response || xhr.responseText);
} else {
reject({
status: xhr.status,
statusText: xhr.statusText,
xhr // <--- This could be useful in case we need more information about the response.
});
// Or just:
// reject(xhr);
}
};
In my case I could parse xhr.response
to get the information I need. I think this could be good for all since sometimes we need to send back a response with specific error messages for multiple fields.