Skip to content

Instantly share code, notes, and snippets.

@DanielDornhardt
Last active August 29, 2015 14:14
Show Gist options
  • Save DanielDornhardt/39b5fb5f9f12d9390f25 to your computer and use it in GitHub Desktop.
Save DanielDornhardt/39b5fb5f9f12d9390f25 to your computer and use it in GitHub Desktop.
Autoform - How could I make form fields visible depending on other values in the form?

How could I make form fields visible depending on other values in the form?

Eg. in the following example I'd like to see the "afQuickField name=vehicle_type_car_num_wheels" only if the selected vehicleType would be 'car' and "afQuickField name=vehicle_type_plane_num_wings" only if vehicleType would be "plane".

The schema could look like this:

vehicleType:
  type: String
  label: 'Type of Vehicle'
  allowedValues: ['car', 'plane']

  autoform:

    options: [

       {label: "Car", value: "car"}

       {label: "Plane", value: "plane"}

    ]

vehicle_type_car_num_wheels:

  type: Number

  label: 'How many wheels?'

  defaultValue: 4


vehicle_type_plane_num_wings:

  type: Number
    label: 'How many wings?'
    defaultValue: 2

The form:

{{#autoForm id="vehicleConfiguration" collection='Vehicles' doc=vehicle type='update' }}
    {{> afQuickField name="vehicleType"}}
    {{> afQuickField name="vehicle_type_car_num_wheels" placeholder="schemaLabel"}}
    {{> afQuickField name="vehicle_type_plane_num_wings" placeholder="schemaLabel"}}
{{/autoForm}}

Pseudocode of what kind of solution I'd like best:

{{#autoForm id="vehicleConfiguration" collection='Vehicles' doc=vehicle type='update' }}
    {{> afQuickField name="vehicleType"}}
    {{#if afGetValue "vehicleType", "car"}}
        {{> afQuickField name="vehicle_type_car_num_wheels" placeholder="schemaLabel"}}
    {{/if}}
    {{#if afGetValue "vehicleType", "plane"}}
        {{> afQuickField name="vehicle_type_plane_num_wings" placeholder="schemaLabel"}}
    {{/if}}
{{/autoForm}}


The code should work for both insert and update forms (type="method" forms would be nice as well, of course).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment