The 'if' tests for title and id seem to be inverted in theme_form_element. Note if there is no title then the code never emits a for= for the id.

This function lives in theme.inc.

Comments

quicksketch’s picture

Here's the offending code from 4.7.4:

function theme_form_element($title, $value, $description = NULL, $id = NULL, $required = FALSE, $error = FALSE) {

  $output  = '<div class="form-item">'."\n";
  $required = $required ? '<span class="form-required" title="'. t('This field is required.') .'">*</span>' : '';

  if ($title) {
    if ($id) {
      $output .= ' <label for="'. form_clean_id($id) .'">'. t('%title: %required', array('%title' => $title, '%required' => $required)) . "</label>\n";
    }
    else {
      $output .= ' <label>'. t('%title: %required', array('%title' => $title, '%required' => $required)) . "</label>\n";
    }
  }

  $output .= " $value\n";

  if ($description) {
    $output .= ' <div class="description">'. $description ."</div>\n";
  }

  $output .= "</div>\n";

  return $output;
}

It looks okay to me. If there isn't a title, then it can't generate a label at all. If there is a title, but not an "id" attribute, then the label tag is generated but doesn't include a for="id-of-element-here". Is this not the problem described?

bmargulies’s picture

If there's no title, why not put the id on what gets created? Here's what I put in my theme, since lots of elements in a webform end up title-less (like checkboxes). I confess that I'm not at all clear on what the use of the 'for' attribute is in a label.


// we override this to get css-able id's on the elements.
function phptemplate_form_element($title, $value, $description = NULL, $id = NULL, $required = FALSE, $error = FALSE) {

    if($id) {
        $output  = '<div class="form-item" id="form-item-' . form_clean_id($id) . '">'."\n";
    } else {
        $output  = '<div class="form-item">'."\n";
    }
  $required = $required ? '<span class="form-required" title="'. t('This field is required.') .'">*</span>' : '';

  if ($title) {
    if ($id) {
      $output .= ' <label for="'. form_clean_id($id) .'">'. t('%title: %required', array('%title' => $title, '%required' => $required)) . "</label>\n";
    }
    else {
      $output .= ' <label>'. t('%title: %required', array('%title' => $title, '%required' => $required)) . "</label>\n";
    }
  }

  $output .= " $value\n";

  if ($description) {
    $output .= ' <div class="description">'. $description ."</div>\n";
  }

  $output .= "</div>\n";

  return $output;
}

bmargulies’s picture

Sorry, I should also have noted that you correctly diagnosed that my complaint was not well-formed. I should have titled 'if you can't id a label, how about id-ing the div'?

walkah’s picture

Project: Forms (obsolete) » Drupal core
Version: 4.7.x-1.x-dev » 4.7.x-dev
Component: Code » forms system
Freso’s picture

Version: 4.7.x-dev » 7.x-dev
Status: Active » Postponed (maintainer needs more info)

Is this still applicable in latest HEAD? (Or in Drupal 5 or 6?)

johnalbin’s picture

Status: Postponed (maintainer needs more info) » Fixed
johnalbin’s picture

Status: Fixed » Closed (duplicate)

Actually, I should say this is a duplicate. Of what, I don't know.