3

I have a form that asks the user to answer several yes or no health questions. These questions are radio button widgets and what I've been trying to do is ask the user to fill out a follow up question, a text area which should be invisible until when the user selects the 'Yes' radio button. I have one method which involved some Javascript, but my I'm mostly a backend developer so I'm really struggling.

Anyone know how I can do this?

1
  • I had the same issue a couple of days ago and the best thing to do is either use the state variable or use ajax in forms. check this video on youtube. It exactly does what you want. The video also explain you how to do it and the basic steps to follow.
    – Jayaram
    Commented May 18, 2012 at 18:23

2 Answers 2

8
+50

Drupal 7 introduced the #states property for form elements. It:

Adds JavaScript to the element to allow it to have different active states.

Here's a short example that applies to your use case:

function MYMODULE_my_form($form, &$form_state) {
  $form['my_radios'] = array(
    '#type' => 'radios',
    '#title' => 'Radios',
    '#options' => array('no' => 'No', 'yes' => 'Yes'),
    '#default_value' => 'no'
  );

  $form['textarea'] = array(
    '#type' => 'textarea',
    '#title' => 'Textarea',
    '#states' => array(
      'visible' => array(
        ':input[name="my_radios"]' => array('value' => 'yes')
      )
    )
  );

  return $form;
}

The above assumes you're building the form yourself; if you're not, you can combine this logic with a hook_form_alter() function. This example assumes you're adding it to a node add/edit form for the Basic Page content type:

function MYMODULE_form_page_node_form_alter(&$form, &$form_state, $form_id) {
  $form['textarea']['#states'] = array(
    'visible' => array(
      ':input[name="name_of_radios_element"]' => array('value' => 'yes')
    )
  );
}

If you're using the second method you'll need to do some digging into the $form array to find out exactly which element you need to target. If you don't already have it I recommend installing the Devel module and using it's excellent dpm() function for your debugging.

There's more information on the various different options for #states on the drupal_process_states() docs page.

3
  • +1, #states is intended to solve this type of problem.
    – Letharion
    Commented May 19, 2012 at 10:45
  • This is like the third time I've gone back to this question for reference. Uberhelpful.
    – Mike
    Commented Aug 8, 2012 at 10:30
  • @Mike Hah, I'd forgotten all about it...and instead keep referring back to the official docs :P
    – Clive
    Commented Aug 8, 2012 at 10:32
2

May be this modules can help:

  • Conditional Fields
    Conditional Fields allows you to manage sets of dependencies between fields. When a field is "dependent", it will only be available for editing and displayed if the state of the "dependee" field matches the right condition. When editing a node (or any other entity type that supports fields, like users and categories), the dependent fields are dynamically modified with the States API. You can, for example, define a custom “Article teaser" field that is shown only if a "Has teaser" checkbox is checked.

  • Webform Conditional (Same Page Conditionals)
    This modules enhances the existing Conditional Rules of the Webform Module. It adds the ability to make components dependent on select components (i.e. listboxes, radio buttons, and checkboxes) that appear on the same page.

    Webform already has support for conditional fields on multi-page forms. For example Field C on Page 2 can be dependent on Field A on Page 1. This module adds the ability to have conditional fields on the same page. This module adds the ability to have Field B on page 1 be dependent on Field A also on Page 1.

1
  • Conditional Fields sounds really interesting. I'm going to try Clive's solution just because I need to do A LOT of dependencies. I think it will just be quicker to do with code vs. a module. Thanks anyways.
    – Mike
    Commented May 24, 2012 at 13:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.