2
votes

I'm trying to create a form in order to insert a new element inside a nested array in a collection. Here are my schemas :

Schemas.CampaignsSchema = new SimpleSchema({
  'name': {
    type: String
  }
});
​
Schemas.ElectionsSchema = new SimpleSchema({
  'campaigns': {
    type: [Schemas.CampaignsSchema],
    defaultValue: []
  }
});

Here is my template :

Template.campaignsNew.helpers({
  schema() { return Schemas.CampaignsSchema; },
});
​
​
<template name="campaignsNew">
  {{#autoForm
    collection='Elections'
    schema=schema
    doc=doc
    scope='campaigns'
    id='insertCampaignForm'
    type='update-pushArray'}}
    <fieldset>
      <legend>Add a Campaign</legend>
      {{> afQuickField name='campaigns.$.name'}}
    </fieldset>
    <button type="submit" class="btn btn-primary">Insert</button>
  {{/autoForm}}
</template>

So a field is generated by autoform but nothing happens when I hit submit.

If I enable Autoform.debug() I got :

SimpleSchema.clean: filtered out value that would have affected key "campaigns", which is not allowed by the schema

SimpleSchema.clean: filtered out value that would have affected key "campaigns.$", which is not allowed by the schema

SimpleSchema.clean: filtered out value that would have affected key "campaigns.$.name", which is not allowed by the schema

Does someone have any idea?

1

1 Answers

0
votes

It seems that the schema attribute of #autoform doesn't work with the type update-pushArray.

Here is the template that works with the reste of the code :

<template name="campaignsNew">
  {{#autoForm
    collection='Elections'
    doc=election
    id='insertCampaignForm'
    type='update-pushArray'
    scope='campaigns'}}
    <fieldset>
      <legend>Add a Campaign</legend>
      {{> afQuickField name='name'}}
    </fieldset>
    <button type="submit" class="btn btn-primary">Insert</button>
  {{/autoForm}}
</template>

The only issue is that the name field is pre-filled with the Election name...

It seems that your nested document mustn't have a field with the same name as the main document.

Yet the created nested document has the good name and the main document's name remain unchanged.