Designing Dynamic Forms

Hello folks,

I have the need to design some dynamic forms:

  • If a user chooses a different option on a dropdown the form fields will change

Out of the box I do not see a way to do this. Can anyone give me a suggestion on how to tackle this project(s)?

  Discussion posts and replies are publicly visible

Parents Reply Children
  • In general if I pick a different input on a dropdown field, the form might change on the same section or in different sections with new text fields and dropdowns and radio buttons.

  • +1
    Certified Lead Developer
    in reply to antoniom0004

    A simple example to try:

    a!localVariables(
      local!dropdownValue: tointeger(null()),
      local!orangeChoiceValue,
      local!appleChoiceValues,
      
      local!dropDownChoices: {
        {
          id: 1,
          label: "Apples"
        },
        {
          id: 2,
          label: "Oranges"
        }
      },
      
      a!formLayout(
        label: "Dynamic Form Example",
        contents: {
          a!dropdownField(
            label: "Select a Fruit",
            choiceLabels: local!dropDownChoices.label,
            choiceValues: local!dropDownChoices.id,
            value: local!dropdownValue,
            saveInto: {
              local!dropdownValue,
              
              /* revert the subsequent dropdown values here when the main dropdown is changed */
              a!save(
                local!appleChoiceValues,
                {}
              ),
              a!save(
                local!orangeChoiceValue,
                tointeger(null())
              )
            },
            placeholderLabel: "--select one--"
          ),
          
          a!sectionLayout(
            label: "Apple Selection",
            showWhen: local!dropdownValue = 1,
            contents: {
              a!checkboxField(
                label: "Select Varieties",
                choiceLabels: {
                  "Golden Delicious",
                  "Red Delicious",
                  "Granny Smith"
                },
                choiceValues: {1, 2, 3},
                value: local!appleChoiceValues,
                saveInto: {
                  local!appleChoiceValues
                }
              )
            }
          ),
          a!sectionLayout(
            label: "Orange Features",
            showWhen: local!dropdownValue = 2,
            contents: {
              a!dropdownField(
                label: "Favorite Orange Product",
                choiceLabels: {
                  "Orange Juice",
                  "Orange Peel",
                  "Orange Slices",
                  "Just Oranges"
                },
                choiceValues: {1, 2, 3, 4},
                value: local!orangeChoiceValue,
                saveInto: {
                  local!orangeChoiceValue
                },
                placeholderLabel: "--Select--"
              )
            }
          )
        }
      )
    )

  • Everything needs to be hard coded, no easy way out on this one... I have quite a few field changes... 

  • 0
    Certified Lead Developer
    in reply to antoniom0004

    The example I posted is overly simplified (basically what I could whip up in 5 minutes of my off time) but the same principles can be applied to scale it up to something dramatically more complex.  Can you clarify what you mean by "everything needs to be hard coded", though?

  • All the code needs to be manually entered, I will not use another interface just to drag and drop the fields.
    I do understand what you did, I just thought Appian had an easier GUI to make dynamic forms happen, that's all.
    Thanks again Mike.

  • 0
    Certified Lead Developer
    in reply to antoniom0004

    Well, technically everything I did there should be doable using the GUI form designer, though as I mentioned previously most of us who are very familiar with form design can quite simply do things faster just banging out the "code" (such as it is) manually. 

    A "compromise" approach that might interest you, involves creating a new interface and then dragging the various layouts, sections, columns, and components into approximately the configuration you want (very fast), then for the dynamic code, switch over to "expression view" and enter the more complicated logic (the "show when" variables, more involved "save into" targets, etc) from there.  That way you'd be able to get the structure and setup in place largely without needing to hand-write everything, but have the flexibility of the direct-coding approach when needed.  I do think it's worthwhile learning both approaches, for what it's worth.

  • Exactly my thoughts Mike. Really appreciate the help.