I'm having trouble adding new nested/array values to a collection using autoForm.
I'm trying to use a quickForm to update questions. I'd like the user to be able to add more answer options. My schema looks like this (simplified to omit order, some metadata, etc):
questionSchema = new SimpleSchema({
label: {
type: String
},
answers: {
type: Array,
minCount: 2,
maxCount: 6
},
"answers.$": {
type: Object
},
"answers.$._id": {
type: String,
regEx: SimpleSchema.RegEx.Id,
autoValue: function(){ return Random.id(); },
autoform: {
type: "hidden"
}
},
"answers.$.label": {
type: String,
regEx: /.{1,150}/,
autoform: {
label: false
}
},
"answers.$.count": {
type: Number,
defaultValue: 0,
autoform: {
type: "hidden"
}
}
});
Other than answers.$.label
, I was not using any autoform
options when I was just adding questions via a quickForm type='insert'
. I added those options when I wanted to edit questions because otherwise I got a complaint that I'd left count
null. So I made them hidden but let them be in the form.
My edit form looks like this:
{{> quickForm collection="Questions" id="editQuestionForm"
type="update" setArrayItems="true" doc=questionToEdit
fields="label, answers"}}
I'm currently able to update the labels for my question and any answers that I initially added. But I can't add new answer options. When I do that, it's denied because count
is not optional. But I specified a defaultValue
...
I would rather my quickForm look like this so that I'm not putting the count
s or _id
s where the user could change them:
{{> quickForm collection="Questions" id="editQuestionForm"
type="update" setArrayItems="true" doc=questionToEdit
fields="label, answers, answers.$.label"}}
But maybe I need to keep answers.$._id
there and hidden to ensure my changes update the right answers?
So:
My answer counts default to 0 on insert, so why doesn't that happen when I edit and add answers?
Can autoForm do an upsert instead of an update? Insert new questions, update existing question labels, use
defaultalue
orautoValue
as needed.Should I use a method for this type of thing?
omitFields="'answers.$._id','answers.$.count'"
, instead of afields
property – pfkurtzomitFields
I still get "Count is required" for the initial answers when I try to update the question. – roblinglecount
optional, things almost sort of work. The_id
is actually put into theid
of the inputs on the form, so that seems ok. If I add answers, they get_id
s, but all of thecount
properties are removed and the new answer doesn't get one either... So, again, I could use a before update hook but that seems like a hack and it wouldn't work with nested arrays where I did not want to reset attributes. – roblingle