1

How can I add a radio buttons with Yes/No options instead of the checkbox in the tableselect form (api)?

Here is the code in form.inc on line (3376):

$element[$key] = array(
        '#type' => 'checkbox',
        '#title' => $title,
        '#title_display' => 'invisible',
        '#return_value' => $key,
        '#default_value' => isset($value[$key]) ? $key : NULL,
        '#attributes' => $element['#attributes'],
      );

I changed the code like this:

$element[$key] = array(
        '#type' => 'radios',
        '#title' => $title,
        '#title_display' => 'invisible',
        '#options' => array(
          '1'   => t('Yes'),
          '0' => t('No'),
        ),
        '#return_value' => $key,
        '#default_value' => isset($value[$key]) ? $key : NULL,
        '#attributes' => $element['#attributes'],
      );

When I run it, it shows the form perfectly. But when I click the Submit button it gives me this error:

"An illegal choice has been detected. Please contact the site administrator."

Any help?

3
  • Rule one using Drupal CMS... Never Ever change core files..
    – Anil Sagar
    Commented Mar 11, 2014 at 13:47
  • Please explain why you would like to change above code.. Where exactly your field is getting rendered ? Tell me more about the field you would like to change.. There are many other ways to solve your problem instead of changing core files..
    – Anil Sagar
    Commented Mar 11, 2014 at 14:05
  • Hi Anil. Please guide me what is the alternate way to put a radio button instead of the checkbox in the tableselect Commented May 29, 2014 at 5:48

2 Answers 2

1

You need to set '#multiple' => FALSE on the tableselect where you declare it. This will set the tableselect to use radios rather than checkboxes.

0

#return_value is not a valid attribute for #type' => 'radios'(see Form API ). Try without that line.

And be aware of Anil Sagar comment : "Rule one using Drupal CMS... Never Ever change core files" Means if you need to add radio buttons somewhere, do that in a custom module ...

3
  • 1
    It's not at all suggested to alter drupal core...
    – Anil Sagar
    Commented Mar 11, 2014 at 14:04
  • Please mind the language you use in stackexchange.. I have given explanation in comment.. :-)
    – Anil Sagar
    Commented Mar 11, 2014 at 14:06
  • I have removed the return_value from the code but still it is giving the same "An illegal choice has been detected. Please contact the site administrator." Commented May 29, 2014 at 5:49

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.