background image
HomeRecent PostsDrupalSearchTagsRSSContactAboutAccount

Showing a form legend for required fields

Eric.London's picture

To improve usability, I wanted to add a note to the bottom of my form to show that an asterisk indicates a required form field. In this case, I am modifying the user registration form, but the code can be simplified to work with any form API.

<?php
function MYMODULE_form_alter($form_id, &$form) {
 
// ...code...
 
if ($form_id == 'user_register') {
   
$form['requiredNote'] = array(
     
'#value' => "
        <div class='form-item'>
          <label>
            <span class='form-required'>*</span> Indicates Required Field
          </label>
        </div>"
,
     
'#weight' => 10,
    );
  }
 
// ...code...
}
?>