0

I have programmed module having a form (using Drupals's form API) which contains radio checkboxes like this:

$form['r_8'] = array(
    '#type' => 'radios',
    '#options' => array(
        '1' => t('Yes'),
        '0' => t('No')
    ),
    '#required' => TRUE,
    '#checked' => '', // ???
    '#title' => t('a label text ... '),
    '#default_value' => !empty($form_state['values']['r_8']) ?
        $form_state['values']['r_8'] : '',
    '#suffix' => '<div style="clear: left"></div>'
);

When the page with that form is initially loaded, I want all these radios initially unchecked. I tried to set the #checked property to FALSE, but it doesn't work. Setting the #default_value to, for example, -1, a warning raises (which I can understand, because -1 is none of the provided option values).

Is there any way to load initially the form with the radio's unchecked?

I could not find any information pointer so that's why I ask here.

Thanks in advance!

2

#checked is not a property used from radios form fields, nor radio form fields. It was not even used from Drupal 6.

I used the following code, and none of the radio buttons were selected.

/**
 * Form builder.
 */
function custom_module_form($form, &$form_state) {
  $form['r_8'] = array(
    '#type' => 'radios',
    '#options' => array(
      '1' => t('Yes'),
      '0' => t('No')
    ),
    '#title' => t('Test'),
    '#default_value' => NULL,
  );

  return $form;
}  

screenshot

In this case, if the user doesn't select one of the radio buttons, the form field would not have a a value associated. Your code could handle it as if the user selected a third value (such as "undecided") or what your code considers the default value (e.g. "yes"); if the latter is the case, users should see a note about that.

It makes sense to use the following code, at least in a case: When you want the users voluntary select a value for the field, and not use the default one selected by the module. For example, it would make sense if the user is expressing an agreement, and you want to be sure the user noticed that field, and clicked on one of the radio buttons.

function custom_module_form($form, &$form_state) {
  $form['r_8'] = array(
    '#type' => 'radios',
    '#required' => TRUE,
    '#options' => array(
      '1' => t('Yes'),
      '0' => t('No')
    ),
    '#title' => t('Test'),
    '#default_value' => NULL,
  );

  return $form;
}  
3
  • Seems to me like you always want to get the default value of a form element on a form edit. This isn't a bulletproof solution either. – JurgenR Jul 3 '12 at 15:01
  • It depends; if I want to be sure the users really select one of the radio buttons, I would use the second snippet. The OP just said he doesn't want to have any of the radio buttons selected by default; he didn't explain why. – espero Jul 3 '12 at 15:04
  • @Espero: The why is: My customer "may" want them initially unchecked on page initial load with that form. I agree with JurgenR, that a radio check element must always have an value. Just to be prepared my customer want them initially unchecked, whatever reason he has in his mind, I want to be prepared. If this is wise or right/wrong is another topic. Your snippet no. 2 seems to work fine. I just wonder why the internal drupal validation puts out the empty radio check as "must fill out message" but does not surround the enclosing div of the radio elements with a red frame like the other fields – Andreas W. Wylach Jul 4 '12 at 3:46
1

This works for me:
The default value has to return false.


  $form['r_8'] = array(
    '#type' => 'radios',
    '#options' => array(
        '1' => t('Yes'),
        '0' => t('No')
    ),
    '#required' => TRUE,
    '#title' => t('a label text ... '),
    '#default_value' => !empty($form_state['values']['r_8']) ?
        $form_state['values']['r_8'] : FALSE,
    '#suffix' => ''
  );
3
  • Looks good, but now the validation does not take effect, and does not warn if nothing was checked, because the default value is present as FALSE or 0 ... have no clue how to catch this one in the _validate handler ... – Andreas W. Wylach Jul 3 '12 at 12:11
  • 1
    I guess this will allways cause problems because ideally, radiobuttons are supposed to have a value. Maybe it's not the cleanest but another solution could be to clear the radio elements using a javascript function on window onload. function clear_form_elements(ele){$('ele').find(':input').each(function() { case 'radio': $(this).attr('checked', false);} – JurgenR Jul 3 '12 at 13:24
  • OK, your comment makes sense, they supposed to have a value. I'll leave them checked as with their default. Al other is a bit hacky ... Upvoted your comment! – Andreas W. Wylach Jul 3 '12 at 13:28

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.