Since Drupal 5.11. form fields are wrapped by divs with ids. (Patch was committed to 5.x here: http://drupal.org/node/181831)

But there are at least two issues with this:

1. While the 'id' of the element now gets added a 'wrapper' the 'label for' doesn't. So in html we get a "reference to non-existent ID" error here.

2. Up to now there are no individual ids for each single radio-button. Every radio-button in a set of radio-buttons has the same id. So we get an "ID already defined" error here.

This patch fixes both problems by

  • adding a 'wrapper' to the 'label for' in function theme_form_element and
  • adding the '#return_value' to the id of a radio-button in function form_builder.

I am not sure if these are the right places to do this changes. But for me it works fine and the html-errors are gone.

Please feel free to change this patch for the better.

CommentFileSizeAuthor
#9 344317.patch1.06 KBRobLoach
form_ids.patch1.31 KBsmitty
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

smitty’s picture

Status: Active » Needs review
RobLoach’s picture

Version: 5.12 » 7.x-dev
Status: Needs review » Patch (to be ported)

The labels do have the "for" attribute, they don't have an ID, however. Drupal 7 doesn't have the ID labels, so it'll have to go in there first.

RobLoach’s picture

I also noticed an extra < div > being added right after the form element....

<form id="user-login-form" method="post" accept-charset="UTF-8"
	action="/drupal/head/node?destination=node">
<div> <!-- A DIV without an ID or a class? -->
<div id="edit-name-wrapper" class="form-item"><label
	for="edit-name">Username: <span title="This field is required."
	class="form-required">*</span></label> <input type="text"
	class="form-text required" value="" size="15" id="edit-name"
	name="name" maxlength="60" /></div>
<div id="edit-pass-wrapper" class="form-item"><label
	for="edit-pass">Password: <span title="This field is required."
	class="form-required">*</span></label> <input type="password"
	class="form-text required" size="15" maxlength="60" id="edit-pass"
	name="pass" /></div>
<input type="submit" class="form-submit" value="Log in" id="edit-submit"
	name="op" />
<div class="item-list">
<ul>
	<li class="first"><a title="Create a new user account."
		href="/drupal/head/user/register">Create new account</a></li>
	<li class="last"><a title="Request new password via e-mail."
		href="/drupal/head/user/password">Request new password</a></li>
</ul>
</div>
<input type="hidden" value="form-023a6789665592355c77d570bce84fc3"
	id="form-023a6789665592355c77d570bce84fc3" name="form_build_id" /> <input
	type="hidden" value="user_login_block" id="edit-user-login-block"
	name="form_id" /></div>
</form>
jroth’s picture

I think I have a solution that doesn't include modifying core files...

Placing the following code in my template.php file seems to do the trick.

function phptemplate_form_element($element, $value) {
static $dupe_ids = array();
$output = '

if (!empty($element['#id'])) {
$dupe_ids[$element['#id']]++;
$output .= ' id="'. $element['#id'] .'-wrapper-'.$dupe_ids[$element['#id']].'"';
}
$output .= ">\n";
$required = !empty($element['#required']) ? '*' : '';

if (!empty($element['#title'])) {
$title = $element['#title'];
if (!empty($element['#id'])) {
$output .= ' '. t('!title: !required', array('!title' => filter_xss_admin($title), '!required' => $required)) ."\n";
}
else {
$output .= ' '. t('!title: !required', array('!title' => filter_xss_admin($title), '!required' => $required)) ."\n";
}
}

$output .= " $value\n";

if (!empty($element['#description'])) {
$output .= '

'. $element['#description'] ."

\n";
}

$output .= "

\n";

return $output;
}

smitty’s picture

Nevertheless I think that's a bug in the core and should be fixed there.

sun’s picture

Status: Patch (to be ported) » Postponed (maintainer needs more info)

Is this still an issue? Form labels and checkboxes/radios have been revamped in the meantime.

RobLoach’s picture

I'd consider #495480: Add class to wrapper div of form elements theme_form() a better solution. Labels are also now stuck in a theme function, not sure what else you'd like to do with them.

sun’s picture

Not sure how that issue is related to this issue.

1. While the 'id' of the element now gets added a 'wrapper' the 'label for' doesn't. So in html we get a "reference to non-existent ID" error here.

This is the only part of this issue which may be still valid under certain conditions.

2. Up to now there are no individual ids for each single radio-button. Every radio-button in a set of radio-buttons has the same id. So we get an "ID already defined" error here.

This is no longer the case in D7.

RobLoach’s picture

Status: Postponed (maintainer needs more info) » Needs review
FileSize
1.06 KB

Hmmm, maybe it's just a wrapper element that's missing then?

Status: Needs review » Needs work

The last submitted patch, 344317.patch, failed testing.

sun’s picture

Was that a confirmation that the issue/problem still exists?