1

I'm developing a custom form module in Drupal 7. I would like to wrap my form inputs in the label tags like this:

<div class="form-item form-type-textfield form-item-FirstName">
<label for="edit-firstname">First Name 
<span class="form-required" title="This field is required.">*</span>
<input type="text" id="edit-firstname" name="FirstName" value="" size="25" maxlength="37" class="form-text required" /></label>
</div>

The label closing tag is after the end of the input. Normally it would be after the span closing tag.

I think I'll need to override the 'theme_form_element_label' function in the 'includes/form.inc' but I'm not sure how to go about it.

user1258011
  • 11
  • 1
  • 4

1 Answers1

1

I don't know why you would wrap a form element around a form element. But to answer your question use the '#prefix' and '#suffix' keys to add your label.

So your code may look similar to this:

$form['first_name'] = array(
   '#type' => 'textfield',
   '#prefix' => '<label for="edit-firstname">' . t('First Name'),
   '#suffix' => '</label>',
   '#required' => TRUE
);

If you are trying to put a label next to a textfield all you would need to do is add the '#title' key to your textfield element. So it may look like this:

$form['first_name'] = array(
   '#type' => 'textfield',
   '#title' => t('First Name'),
   '#required' => TRUE
);
samwell
  • 2,757
  • 9
  • 33
  • 48