2016-05-19 18 views
0

投稿にカテゴリを追加しようとしているので、ユーザーが自分の投稿に関連付けるカテゴリを選択できる選択ボックスがあるフォームがあります。問題は、フォームのカテゴリがオブジェクトではないため、フォームを送信するときにエラーが発生していた... formToDocというフォームをフックしてdocを変更し、カテゴリ要素の外観をよくするが動作しない。私は私のデータの検証に対処しなければならないと思っていますが、私はどのようにわかりませんか?Meteor - 選択したフォームからコレクションにオブジェクトを追加する

AutoForm.hooks({ 
    createCPDM: { // ID du formulaire 
     formToDoc: function(doc) { 
      var categoryName = $("[name='category'] option:selected").text(); 
      doc.category = {name:categoryName, value:doc.category}; 
      return doc; 
     }, 
     onSubmit: function (doc) { // Gestion du formulaire de soumission 
      var error = null; 
      var title = doc.title; 
      var content = doc.content; 
      var category = doc.category; 
      var captcha = $("#captcha").val(); 

      var formData = { 
       title: title, 
       content: content, 
       category: category 
      }; 

      if (captcha == 4) { 
       Meteor.call('createCPDM', formData, function (err) { 
        if (err) { 
         error = new Error("Une erreur s'est produite"); 
        } 
       }); 
      } 
      else { 
       error = new Error("Mauvais captcha"); 
      } 

      if (error === null) { 
       this.done(); // Appelle onSuccess 
      } 
      else { 
       this.done(error); // Appelle onError 
      } 

      return false; 
     }, 

感謝の手助けのために:

Category = new Mongo.Collection("category"); 

// Création du schéma des catégories 
Category.attachSchema(new SimpleSchema({ 
    name: { 
     type: String, 
     label: "Catégorie", 
     max: 200 
    }, 
    value: { 
     type: String, 
     label: "Catégorie Value", 
     max: 200 
    } 
})); 

// Création du schéma des CPDM 
CPDM.attachSchema(new SimpleSchema({ 
    title: { 
     type: String, 
     label: "Titre", 
     max: 200 
    }, 
    content: { 
     type: String, 
     label: "Contenu", 
     autoform: { 
      afFieldInput: { 
       type: "textarea", 
       rows: 15 
      } 
     } 
    }, 
    createdAt: { 
     type: Date, 
     autoform: { 
      omit: true 
     }, 
     autoValue: function() { 
      if (this.isInsert) { 
       return new Date; 
      } 
      else { 
       this.unset(); 
      } 
     } 
    }, 
    author: { 
     type: String, 
     autoform: { 
      omit: true 
     }, 
     autoValue: function() { 
      if (this.isInsert) { 
       if (Meteor.user()) { 
        return Meteor.user().username; 
       } else { 
        return "Anonyme"; 
       } 
      } else { 
       this.unset(); 
      } 
     } 
    }, 
    ranking: { 
     type: Number, 
     autoValue: function() { 
      if (this.isInsert) { 
       return 0; 
      } 
     }, 
     autoform: { 
      omit: true 
     }, 
     min: 0, 
     label: "Note" 
    }, 
    voters: { 
     type: [String], 
     autoform: { 
      omit: true 
     }, 
     autoValue: function() { 
      if (this.isInsert) { 
       return []; 
      } 
     } 
    }, 
    selected: { 
     type: Boolean, 
     autoform: { 
      omit: true 
     }, 
     autoValue: function() { 
      if (this.isInsert) { 
       return false; 
      } 
     } 
    }, 
    category: { 
     type: Object, 
     optional: true, 
     label: "Catégorie", 
     autoform: { 
      firstOption: "Sélectionner une catégorie" 
     } 
    } 
})); 

そして、ここでは私のformHookです:

は、ここに私のコレクションのCPDMとカテゴリです!

答えて

0

解決策が見つかりました。投稿コレクションを調整して、カテゴリをオブジェクトにする必要がありました。このように:

category: { 
     type: Object, 
     optional: true, 
     label: "Catégorie", 
     autoform: { 
      firstOption: "Sélectionner une catégorie" 
     } 
    }, 
    "category.name": { 
     type: String, 
     label: "Catégorie", 
     max: 200, 
     autoform: { 
      omit: true 
     } 
    }, 
    "category.value": { 
     type: String, 
     label: "Catégorie Value", 
     max: 200, 
     autoform: { 
      omit: true 
     } 
    } 
関連する問題