Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autoform: How to dynamically show and add fields of a sub-schema depending on another field?

What is the best approach to dynamically show fields of a sub-schema (Object) depending on another field? In the following example a document (Schemas.Main) can include several items defined in Schemas.Items. The fields that are needed to fill in items are dependendent on the selected type.

For example if a user selects type=="type1", fields "type1_field1" and "type1_field2" need to be filled.

A solution probably needs to use autoForm and combine AutoForm.getFieldValue and setting fields of an afArrayField, correct? I have tried a lot of combinations but either the ability to add additional items is lost (missing plus-sign) or I cannot add different items (i.e. all items are type1). Any hints how to solve this?

//Schemas.js
Schemas = {}; Collections = {};
Main = Collections.Main = new Mongo.Collection("Main");
Main.attachSchema(Schemas.Main);
Meteor.isClient && Template.registerHelper("Schemas", Schemas);

Schemas.Main = new SimpleSchema({
  name: {
    type: String
  },
  items: {
    type: [Schemas.Items]
  }
});

Schemas.Items = new SimpleSchema({
      type: { //FormActions...
          type: String,
          allowedValues: ['type1', 'type2', 'type3'],
          autoform: {
              type: "selectize"
          }
      },
      type1_field1: {
        type: String
      },
      type1_field2: {
        type: String
      },
      type2_field1: {
        type: String
      },
      type2_field2: {
        type: String
      },
      type3_field1: {
        type: String
      },
      type3_field2: {
        type: String
      }            
});

//form.html
{{#autoForm id="testForm" type="insert" collection=Collections.Main}}
     {{> afFieldInput name='name'}}

     {{> afArrayField name='items' fields="items.$.type, items.$.type1_field1"}} //How to set these fields dynamically depending on type?

    <div class="form-group">
      <button type="submit" class="btn btn-primary">Create</button>
    </div>
{{/autoForm}}  
like image 309
Miriam Avatar asked Mar 11 '15 17:03

Miriam


1 Answers

I finally used another approach and created an own template based on afArrayField, which uses

{{> UI.dynamic template=currentFieldValue}}

Not sure if this is the best approach but seems to be working for my situation:

Template.registerHelper("currentFieldValue", function() {
  return AutoForm.getFieldValue("testForm", this.current.type) || "no type so far";
});



{{#autoForm id="testForm" type="insert" collection=Collections.Main}} 
  {{> afFieldInput name='name'}} 
  {{> afArrayField name='items' id="something" template="mycustom"}}
  <div class="form-group">
    <button type="submit" class="btn btn-primary">Create</button>
  </div>
{{/autoForm}}

<template name="afArrayField_mycustom">
  <div class="panel panel-default">
    <div class="panel-heading">{{afFieldLabelText name=this.atts.name}}</div>
    {{#if afFieldIsInvalid name=this.atts.name}}
    <div class="panel-body has-error">
      <span class="help-block">{{{afFieldMessage name=this.atts.name}}}</span>
    </div>
    {{/if}}
    <ul class="list-group">
      {{#afEachArrayItem name=this.atts.name minCount=this.atts.minCount maxCount=this.atts.maxCount}}
      <li class="list-group-item autoform-array-item">
        <div>
          <div class="autoform-remove-item-wrap">
            {{#if afArrayFieldHasMoreThanMinimum name=../atts.name minCount=../atts.minCount maxCount=../atts.maxCount}}
            <button type="button" class="btn btn-primary autoform-remove-item"><span class="glyphicon glyphicon-minus"></span>
            </button>
            {{/if}}
          </div>
          <div class="autoform-array-item-body">
            <!--all actions have a type -->
            {{> afFieldInput name=this.current.type label=false options="auto"}}
            <!--branch here for other fields that depend on type -->
            {{> UI.dynamic template=currentFieldValue}}
          </div>
        </div>
      </li>
      {{/afEachArrayItem}} 
      {{#if afArrayFieldHasLessThanMaximum name=this.atts.name minCount=this.atts.minCount maxCount=this.atts.maxCount}}
      <li class="list-group-item">
        <button type="button" class="btn btn-primary autoform-add-item" data-autoform-field="{{this.atts.name}}" data-autoform-minCount="{{this.atts.minCount}}" data-autoform-maxCount="{{this.atts.maxCount}}"><span class="glyphicon glyphicon-plus"></span>
        </button>
      </li>
      {{/if}}
    </ul>
  </div>
</template>

<template name="type1">
  <!--include type1 fields here-->
</template>

<template name="type2">
  <!--include type2 fields here-->
</template>

<template name="type3">
  <!--include type3 fields here-->
</template>
like image 71
Miriam Avatar answered Sep 30 '22 13:09

Miriam