1

I need to get radio buttons in a form, whenever a checkbox gets enabled/clicked. Say, I have 5 items listed in a checkbox, named orange, apple, .....When I enable orange checkbox, a set of radio button comes that will ask color of the corresponding fruit. Any working example regarding this will be highlyy appreciated.

EDIT:

$form['toggle_me'] = array(
  '#type' => 'checkboxes',
  '#title' => t('Tick this box to type'),
   '#options' => array(0 => t('Orange'), 1 => t('Apple')),
);
$form['settings'] = array(
  '#type' => 'radios',
  '#states' => array(
    // Only show this field when the 'toggle_me' checkbox is enabled.
    'visible' => array(
      ':input[name="toggle_me"][#options][0]' => array(0 => t('red'), 1 => t('yellow')),
    ),
  ),
);
1

Try This, may be helpful for you

$form['checkbox'] = array(
    '#title' => 'Select',
    '#type' => 'checkboxes',
    '#options' => drupal_map_assoc(array(t('Orange'), t('Apple'))),
    '#ajax' => array(
      'callback' => 'fruits_callback_form',
      'wrapper' => 'fruit-check',
      'method' => 'replace',
      'effect' => 'fade',
    ),
  );
  $checkbox = !empty($form_state['values']['checkbox']) ? $form_state['values']['checkbox'] : 0;
  $form['slots']['#prefix'] = '<div id="fruit-check">';
  if ($checkbox == 'Orange') {
    $color = array(0 => t('Red'), 1 => t('Blue'));
  }
  $form['color'] = array(
    '#type' => 'radios',
    '#title' => t('Select Color'),
    '#options' => $color,
  );
  $form['slots']['#suffix'] = '</div>';


  //Callback function

  function fruits_callback_form($form, &$form_state) {
    return $form['color'];
  }
  • 1
    can I write radio form element in ajax callback function?Because I want radio buttons to get visible only if check-box gets enabled. – Sugandh Khanna Aug 11 '15 at 7:17
  • 1
    then use !empty($form_state['values']['checkbox']) in form. If not empty then show your radio buttons. Otherwise as you said, you can define in callback function also – Ajay Reddy Aug 11 '15 at 8:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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