1
votes

I have this quickForm:

{{> quickForm id="insertFixie" collection="Fixies" type="insert" doc=seedObject}}

Backed up by this schema:

Fixies = new Meteor.Collection('fixies');
Schema.Fixies = new SimpleSchema({
    description: {
        type: String,
        label: "Description",
        trim: true,
        optional: true
    },
    cost: {
        type: Number,
        label: "Cost",
        min: 0,
        decimal: true
    },
    product_id: {
        type: String,
        autoform: {
            omit: true
        }
    },
});
Fixies.attachSchema(Schema.Fixies);

and this seedObject method:

Template.insertFixie.helpers({
    seedObject: function () {
        console.log({product_id: this._id});
        return {product_id: this._id};
    }
});

When that console call directly above happens, it's correct and gives something to this effect:

Object {product_id: "1"}

But when I submit the form with something valid (like "stuff" and "100"), I get this error:

insert error:
Error: Product is required {invalidKeys: Array[1],
validationContext: SimpleSchemaValidationContext,
stack: (...),
message: "Product is required"}

stating that the product_id attribute is required and currently has a value of null.

What am I doing wrong? That product_id is a template dependent value, so something like "autoValue" in the schema doesn't seem like the best way to handle it.

The docs seem to clearly state that I'm using things correctly. From the description of the doc attribute of Auto Form:

For an insert form, you can also use this attribute to pass an object that has default form values set (the same effect as setting a value attribute on each field within the form).

And from the description of the value attribute of afFieldInput:

value: Set a specific, potentially reactive, value for the input. If you have also provided a doc attribute on the autoForm or quickForm, this value will override the value from the doc object.

What am I missing?

Edit

I added an autoValue field to my schema, just to see what pops up:

autoValue: function (doc) {
    console.log(doc)
    console.log(this.value)
    return "1";
}

This allows the form to correctly submit, but with the incorrect hard-coded value of "1" rather than a useful value from the template. The two console logs show this:

:24 Object {description: "stuff", cost: 50}
:25 undefined

It seems my seedObject value isn't available to autoValue.

Do I have to hijack the onSubmit hooks? Do I have to have hidden form inputs with values supplied from the template? What's the fix here?

1

1 Answers

2
votes

It turned out to be a hidden input.

I expanded my form to this:

{{#autoForm id="insertFixie" collection="Fixies" type="insert"}}
    <fieldset>
        {{> afFormGroup name="description" placeholder="schemaLabel" label=false}}
        <div class="form-group{{#if afFieldIsInvalid name='cost'}} has-error{{/if}}">
            <div class="input-group">
                <div class="input-group-addon">$</div>
                {{> afFieldInput name="cost" placeholder="schemaLabel" label=false}}
            </div>
            {{#if afFieldIsInvalid name="cost"}}
                <span class="help-block">{{afFieldMessage name="cost"}}</span>
            {{/if}}
        </div>
        {{> afFormGroup name="product_id" type="hidden" value=_id}}
    </fieldset>
    <button class="btn btn-primary" type="submit">Insert</button>
{{/autoForm}}

Adding an afFormGroup with type="hidden" did exactly the trick.

Although it still seems to me like the doc argument isn't living up to it's promises.