Skip to main content

Drupal 7 - Customising the required form fields markup

Published on 26th November 2012

When theming your Drupal form, sometimes an asterisk is just not good enough to distinguish the required fields. I always like to add text to make it clear to user exactly what is required.

A standard drupal form

The field with * beside it represents a mandatory field. I like to replace * with '(required)'. The theme_form_required_marker() was added in Drupal 7.

In your active theme's template.php file add the following. You can replace the text between the 'span' tags to whatever you wish.

function MYTHEME_form_required_marker($variables) {
  // This is also used in the installer, pre-database setup.
  $t = get_t();
  $attributes = array(
    'class' => 'form-required',
    'title' => $t('This field is required.'),
  return 'required';

You can see an example of this on my contact webform.