Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Forms & Blueprints

Conditional field depending on another radio field selection

form

Started by David 9 years ago · 5 replies · 2037 views
9 years ago

Hi,

I am trying to build a form with conditional fields that depend on a radio selection.
In other words, if user selects one radio option, the conditional fields would show up; on the other hand, if the user selects the other radio option, the conditional fields would hide.

This is the markdown file I've done so far:

YAML
form:
    name: form1
    fields:
        - name: radio_field
          label: Have you ever changed your name?
          type: radio
          options:
              '0': 'No'
              '1': 'Yes'
          validate:
            required: true
        - name-change-condition:
          type: conditional
          condition: "[[CONDITION HERE]] ? 'true' : 'false'"
          fields:
            - name: new-firstname
              type: text
            - name: new-lastname
              type: text

Is this possible using the conditional fields on the form plugin?

Thanks in advance

9 years ago

Conditional field will display a field or not base on a twig condition. This condition is only evaluated on the page load, so if it is a multi-page form (like a poll), it could work, otherwise, you might have to use javascript for this task.

9 years ago

Thanks for your response, Paul.
This is what I thought but I wanted to be sure though.
I will do it with JS then

9 years ago

Hi David, can you share your final solution? I am in need of this too

9 years ago

Hi @parijke,

What do you need exactly?

9 years ago

Hi Paul,

What I need is a form with checkboxes (mortgage, insurance)

Depending on the choice ie mortgage additional form fields need to be shown like Purchase Price and Mortgage Required

Suggested topics

Topic Participants Replies Views Activity
Forms & Blueprints · by Ton Haarmans, 5 years ago
13 1135 4 months ago
Forms & Blueprints · by Hugo Oliveira, 5 months ago
0 60 5 months ago
Forms & Blueprints · by Flachy Joe, 6 months ago
9 132 6 months ago
Forms & Blueprints · by Augustus, 7 months ago
7 108 7 months ago
Forms & Blueprints · by Julien, 7 months ago
10 127 7 months ago