我使用 RESTadpater 来持久化数据。当发生验证错误时,我想返回 422 响应,然后记录错误并在每个错误字段旁边显示指示。
我的 REST 响应状态码如下:
Status Code:422 Unprocessable Entity
我的 REST 响应正文如下:
{
"message": "Validation failed",
"errors": [
{
"name": "duplicate"
}
]
}
在我的控制器中, becomeInvalid 正确触发。
App.AuthorsNewController = Ember.ObjectController.extend({
startEditing: function () {
//Create a new record on a local transaction
this.transaction = this.get('store').transaction();
this.set('model', this.transaction.createRecord(App.Author, {}));
},
save: function (author) {
//Local commit - author record goes in Flight state
author.get('transaction').commit();
//If response is success: didCreate fires
//Transition to edit of the new record
author.one('didCreate', this, function () {
this.transitionToRoute('author.edit', author);
});
//If response is 422 (validation problem at server side): becameError fires
author.one('becameInvalid', this, function () {
console.log "Validation problem"
});
}
...
2个问题:
我想在“console.log“验证问题”下面记录服务器返回的完整错误列表。我怎样才能做到这一点 ?
在我的 hbs 模板中,我想在相关字段旁边指出一个错误。我怎样才能做到这一点 ?
我不确定通过 REST 适配器返回的数据是否正确。所以问题可能出在 REST 端或 Ember 端......