If the return value for a checkbox is 0 (zero), then that checkbox will always be selected as default. I believe the root of this issue is to be found in theme_checkbox() on the following line:

if ($element['#value'] !== 0 && $element['#value'] == $element['#return_value']) {

The problem is that if #return_value is 0 and #value is empty, then the loose comparison (==) will result in the two values to be deemed equal. The result is that the particular checkbox will be selected. I believe a possible solution to this problem would be to make sure that #value never is empty, but always defaults to 0 if it is empty. In other words, one could simply do the following:

function theme_checkbox($variables) {
  $element = $variables['element'];

  //Validate #value and ensure that it defaults to 0 if empty.
  $element['#value'] = !empty($element['#value']) ? $element['#value'] : 0; 
  $t = get_t();

Comments

matslats’s picture

plus one

bakr’s picture

plus two