1

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 つの質問:

  1. 「console.log "Validation problem"」の下に、サーバーから返されたエラーの完全なリストを記録したいと思います。どうやってやるの ?

  2. hbs テンプレートで、関連するフィールドの横にエラーを表示したいと考えています。これどうやってするの ?

REST アダプター経由で返されたデータが正しいかどうかはわかりません。したがって、問題はREST側またはEmber側にある可能性があります...

4

2 に答える 2

3

解決:

コントローラの保存機能では:

    author.one('becameInvalid', this, function () {
       console.log "Validation problem"
       this.set('errors', this.get('content.errors'));
    });

hbs テンプレート:

    {{view Ember.TextField valueBinding='name'}} 
    {{#if errors.name}}{{errors.name}}{{/if}}
于 2013-08-08T22:05:57.953 に答える
1

これが私のやり方です。ベストプラクティスではないかもしれませんが、私にとってはうまくいきます:

  1. commit() を使用する代わりに、save() を使用します。何が違うのか疑問に思われる場合は、こちらのリンクをご覧ください。トランザクションを使用するアプローチは試していませんが、基本的には record = App.Model.createRecord(...) を使用してレコードを作成します。AddShopController 内の apiAddShop 関数のコードは次のとおりです。

    apiAddShop: function() {
    //console.log("add shop");
    newShop = App.Shop.createRecord({name:this.get('name'), currentUserRole:"owner"});
    
    //this.get('store').commit(); // Use record.save() instead, then() is not defined for commit()
    var self = this;
    newShop.save().then(function(response){
        self.transitionToRoute('shops');
    }, function(response){
        // if there is error:
        // server should respond with JSON that has a root "errors"
        // and with status code: 422
        // otherwise the response could not be parsed.
        var errors = response.errors;
        for(var attr in errors){
            if (self.hasOwnProperty(attr)) {
                self.set(attr+"Error", true);
                self.set(attr+"Message", Ember.String.classify(attr)+" "+errors[attr]);
            }
            console.log(attr + ': ' + errors[attr]);
        }
        console.log(response.errors.name[0]);
    });
    

    }、

  2. 上記のコードは、フォーム内の各属性に attrError(boolean) と attrMessage(string) があることを前提としています。次に、テンプレートで、フィールドのクラスをこれらのエラー属性 ( など) にバインドできます。エラーメッセージは<div {{bindAttr class=":control-group nameError:error:"}}>、次のようなフォーム フィールドの横に簡単に表示できます。 SO が HTML 入力をエスケープしているため)。<span {{bindAttr class=":help-inline nameError::hidden"}} id="new_shop_error">{{nameMessage}}</span>

于 2013-08-30T06:10:47.943 に答える