Component

Form fieldset

The fieldset element is used to group several controls within a web form. The legend element represents a caption for the content of its parent fieldset.

Using the fieldset and legend elements

You can use the text property or pass text as a block.

Search for usage of this component on GitHub.

How it looks (preview) (preview all)

Do you have a passport?

How to call this component

<%= render "govuk_publishing_components/components/fieldset", {
  legend_text: "Do you have a passport?"
} do %>
  <!-- example content -->
  <%= render "govuk_publishing_components/components/radio", {
    name: "default",
    items: [
      {
        value: "default-yes",
        text: "Yes"
      },
      {
        value: "default-no",
        text: "No"
      }
    ]
  } %>
  <!-- end of example content -->
<% end %>

Accessibility acceptance criteria

  • must give inputs within the fieldset context with legend text

Other examples

Standard options

This component uses the component wrapper helper. It accepts the following options and applies them to the parent element of the component. See the component wrapper helper documentation for more detail.

  • id - accepts a string for the element ID attribute
  • data_attributes - accepts a hash of data attributes
  • aria - accepts a hash of aria attributes
  • classes - accepts a space separated string of classes, these should not be used for styling and must be prefixed with js-
  • margin_bottom - accepts a number from 0 to 9 (0px to 60px) using the GOV.UK Frontend spacing scale (defaults to no margin)
  • role - accepts a space separated string of roles
  • lang - accepts a language attribute value
  • open - accepts an open attribute value (true or false)
  • hidden - accepts an empty string, ‘hidden’, or ‘until-found’
  • tabindex - accepts an integer. The integer can also be passed as a string.
  • dir - accepts ‘rtl’, ‘ltr’, or ‘auto’.

With id attribute (preview)

Do you have a passport?
<%= render "govuk_publishing_components/components/fieldset", {
  legend_text: "Do you have a passport?",
  id: "passports"
} do %>
  <!-- example content -->
  <%= render "govuk_publishing_components/components/radio", {
    name: "passport",
    items: [
      {
        value: "passport-yes",
        text: "Yes"
      },
      {
        value: "passport-no",
        text: "No"
      }
    ]
  } %>
  <!-- end of example content -->
<% end %>

With heading (preview)

Make the legend different sizes. Valid options are s, m, l and xl.

Do you have a driving license?

<%= render "govuk_publishing_components/components/fieldset", {
  legend_text: "Do you have a driving license?",
  heading_level: 2,
  heading_size: "m"
} do %>
  <!-- example content -->
  <%= render "govuk_publishing_components/components/radio", {
    name: "level",
    items: [
      {
        value: "level-yes",
        text: "Yes"
      },
      {
        value: "level-no",
        text: "No"
      }
    ]
  } %>
  <!-- end of example content -->
<% end %>

With custom legend size (preview)

Make the legend different sizes. Valid options are s, m, l and xl.

Do you have a driving license?
<%= render "govuk_publishing_components/components/fieldset", {
  legend_text: "Do you have a driving license?",
  heading_size: "l"
} do %>
  <!-- example content -->
  <%= render "govuk_publishing_components/components/radio", {
    name: "size",
    items: [
      {
        value: "size-yes",
        text: "Yes"
      },
      {
        value: "size-no",
        text: "No"
      }
    ]
  } %>
  <!-- end of example content -->
<% end %>

With error message (preview)

The component also accepts an error_id, or generates one automatically.

Do you have a passport?

Error: Please choose an option

<%= render "govuk_publishing_components/components/fieldset", {
  legend_text: "Do you have a passport?",
  error_message: "Please choose an option"
} do %>
  <!-- example content -->
  <%= render "govuk_publishing_components/components/radio", {
    name: "default2",
    items: [
      {
        value: "default2-yes",
        text: "Yes"
      },
      {
        value: "default2-no",
        text: "No"
      }
    ]
  } %>
  <!-- end of example content -->
<% end %>