meteor - How do you add values to AutoForm's form object without using a form? -
i've got form created through autoform.
as far data sources, can fill in parts of form , use:
autoform.getformvalues('form-id').insertdoc // returns contents of form
when validate form can do:
var formvalues = autoform.getformvalues('form-id').insertdoc; var isvalid = mycollection.simpleschema().namedcontext("mycontext").validate(formvalues); // if isvalid returns true, enable submit button
instead of filling in parts of form, want manually add information whatever object autoform uses validation , submission collection.
for example, there data fields in schema don't need appear in form itself.
take shopping cart:
shoppingcartschema = { totalprice: { type: number, optional: false }, itemsselected: { type: [object], optional: false } };
the data itemsselected
provided through user input on form.
the data totalprice
should not form input. it's generated in code.
but totalprice
still needs validated required field before autoform submits form collection.
so how add totalprice
onto object autoform validates?
you use autovalue if wanted to.
shoppingcartschema = new simpleschema({ 'items': { type: [object], }, 'items.$.name': { type: string }, 'items.$.price': { type: number }, totalprice: { type: number, autovalue: function () { if (this.field('items').isset) { let total = this.field('items').value.reduce(function (sum, item) { return sum + item.price; }, 0); if (this.isinsert) { return total; } else { return { $set: total }; } } } }, });
Comments
Post a Comment