I'm trying to add a Category to my Post, so I have a form with a select box where the user can choose the category to associate with his post. The problem is that when I submit my form, I had an error because the field Category is not an object... I tried to hook the form with formToDoc
to modify the doc
and make the category element look good but it doesn't work. I'm guessing that I have to deal with the validation of my data but I don't know how ?
Here are my collections CPDM and Category :
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"
}
}
}));
And here is my formHook :
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;
},
Thank's for helping !
I found the solution, I had to adjust my Post collection to make category to be an object. Like this :
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
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.