简体   繁体   English

Ember-data:关系验证错误(hasMany)

[英]Ember-data: validation errors on relationships (hasMany)

I'm trying to create record with associated records in one request. 我正在尝试在一个请求中创建包含相关记录的记录。 In case if some of nested records have validation errors, I'd like to access appropriate errors on that record. 如果某些嵌套记录有验证错误,我想访问该记录上的相应错误。 I'm using json-api adaptor, so what should be the format of errors from the backend? 我正在使用json-api适配器,那么后端的错误格式应该是什么? I'm trying something like this, with no luck though: 我正在尝试这样的事情,虽然没有运气:

{"errors":[
  {
    "detail": "can't be blank",
    "source": {
      "pointer":"data/relationships/steps/0/data/attributes/est_threshold"
    }
  }
]}

According to this line, it should be implemented somehow: https://github.com/emberjs/data/blob/master/addon/adapters/errors.js#L7 根据这一行,它应该以某种方式实现: https//github.com/emberjs/data/blob/master/addon/adapters/errors.js#L7

Any ideas? 有任何想法吗?

You'll need to sideload nested records in the data. 您需要在数据中加载嵌套记录。 The example structure given in the ember guides is: 余烬指南中给出的示例结构为:

{
  "post": {
    "id": 1,
    "title": "Node is not omakase",
    "comments": [1, 2, 3]
  },

  "comments": [{
    "id": 1,
    "body": "But is it _lightweight_ omakase?"
  },
  {
    "id": 2,
    "body": "I for one welcome our new omakase overlords"
  },
  {
    "id": 3,
    "body": "Put me on the fast track to a delicious dinner"
  }]
}

https://guides.emberjs.com/v1.10.0/models/the-rest-adapter/ https://guides.emberjs.com/v1.10.0/models/the-rest-adapter/

So it doesn't seem to be implemented yet. 所以它似乎还没有实现。 I found kinda hackish way to do that in the model mixin: 我发现在模型mixin中有一种hackish方式来做到这一点:

`import Ember from 'ember'`

RelatedErrors = Ember.Mixin.create

  save: ->
    @_super().catch (resp) =>
      resp.errors.forEach (err) =>
        if [_, rel, idx, attr] = err.source.pointer.match /^data\/relationships\/(\w+)\/(\d+)\/data\/attributes\/(\w+)$/
          @get(rel).objectAt(idx).get('errors').add(attr, err.detail)

`export default RelatedErrors`

However, add on DS.Errors is deprecated, so this is still not a perfect solution. 但是,不推荐在DS.Errors上add ,因此这仍然不是一个完美的解决方案。 Also invalid state of related models need to be cleared before each commit, which is not happening this far. 在每次提交之前,还需要清除相关模型的无效状态,这种情况到目前为止还没有发生。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM