1

I have a form which has 3 radio buttons, each providing a fieldset.

o Choice 1
  Fieldset of options v
o Choice 2
  Fieldset of options v
o Choice 3
  Fieldset of options v

In order to achieve this, the three radios are each individual radio buttons, and the fieldset for each is enabled when the radio is checked.

The problem, of course, is that once checked, checking one of the other radios doesn't uncheck the original, because they are different fields. I could use #states to simulate radio behavior, clearing one when another is selected, but I don't believe that will work. If I set Option2 to clear if Option1 is checked: Checking Option2, then checking Option1 will clear Option2, great Trying to check Option2 again will leave it unchecked, because the test of whether Option1 is checked will be true

I tried this setting for two of them:

  '#states' => array(
    'unchecked' => array(
      ':input[name="*theOtherOption*"]' => array('checked' => TRUE),
    ),

What happens is the behavior works for the first click of each radio button, clicking 1 turns off 2, clicking 2 turns off 1, and then it stops working and I end up with both clicked.

0

2 Answers 2

1

you should study the Ajax examples module you can follow the examples estructures to build you own code. After doing that your code should look somethink like this:

function test_code_radios_callback($form, $form_state) {
  return $form['radios_fieldset'];
}


function test_code_form($form, &$form_state) {

    $options = array(
        'Name'=>t('Name'),
        'Nickname'=>t('Nickname'),
        'Lastname'=>t('Lastname')
        );

    $form['alias'] = array(
    '#type' => 'radios',
    '#title' => 'Choose your alias',
    '#options' => $options,
    '#default_value' => isset($form_state['values']['alias']) ? $form_state['values']['alias'] : '',
    '#ajax' => array(
      'callback' => 'test_code_autotextfields_callback',
      'wrapper' => 'textfields',
      'effect' => 'fade',
        ),
  );

    $form['textfields'] = array(
    '#title' => t("Your Alias"),
    '#prefix' => '<div id="textfields">',
    '#suffix' => '</div>',
    '#type' => 'fieldset',
    '#description' => t('Write your alias here'),
  );


    if (!empty($form_state['values']['alias']) && $form_state['values']['alias']) {
    $value = array();
    $value = $form_state['values']['alias'];

    foreach($value as $item) {
    $name = $item;
    }

      $form['textfields']['$value'] = array(
      '#type' => 'textfield',
      '#title' => $value,
      );

  }

  return $form;
}
function test_code_autotextfields_callback($form, $form_state) {
  return $form['textfields'];
}

I tested and it's working as i understood you need it. Tell us how it went. Have a good coding!.

0

Try to set the name attribute of your radio buttons to be equal. This is basically just HTML behaviour. All radio buttons with the same name are treated as group and only one can be checked.

$form['myradio1'] = array(
  // ..
  '#attributes' => array(
    'name' => 'myradio',
  ),
);

$form['myradio2'] = array(
  // ..
  '#attributes' => array(
    'name' => 'myradio',
  ),
);

Your Answer

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

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