• Light
  • Dark
  • Auto
  • Default
  • Indigo
  • Blue
  • Green
  • Red
  • Gray

Form group

Form group adds structure to forms.

Examples

Form group
Loading live example...

Structure

TODO:
Add structure image. Create an issue

Guidelines

Use a form group to associate a label or legend, description, help text, and validation feedback with form controls. See the forms pattern for guidance on composing a form.

Appearance

Form groups stack their label above the controls. Use label-description for text directly below the label and description for help text below the controls. Both have named slots for content that includes markup.

Behavior

Labels and grouping

Set label to name the field, or use the label slot for custom content. For a single input, set label-for to the input's unique id, without a leading #. This renders a <div> containing a <label> associated with the input.

For multiple related controls, or checkbox and radio groups with their own input labels, omit label-for. The form group renders a <fieldset> with a <legend> naming the group. Each control still needs its own label. You can nest form groups to label individual fields within a related group:

Set optional to append (optional) to the label, or customize it with optional-text. When using the label slot, include any optional suffix in the slot content yourself.

Disabled groups

The disabled prop disables native form controls inside the rendered <fieldset>. It has no effect when label-for is set, because the group renders a <div>. In that case, set disabled on the input itself.

Validation

Form groups in GitLab pair a label with an input and often description text below the label, and help text below the field, and a validation message that appears once the field is in an invalid state. Bind the same state to both the form group and the input so the message and the input styling stay in sync.

  • false: Invalid. Provide invalid-feedback text explaining how to correct the input.
  • true: Valid. Use valid-feedback only when a success message is useful.
  • null: No validation state.

The invalid-feedback and valid-feedback slots support custom markup. Explicitly setting the group's state makes the corresponding feedback visible even when the input is nested in another component. Feedback appears below the controls and above help text, with aria-live="assertive".

Validated form fields
Loading live example...

Accessibility

Prefer a visible label. If the surrounding context already identifies the field visually, label-sr-only hides the label or legend while keeping its content available to screen readers, including the optional suffix and label description.

With label-for, the form group adds help text and the active validation feedback to the target control's aria-describedby, preserving existing references. The target must be inside the form group.

Without label-for, associate the help text and feedback with each control using ariaDescribedby from the scoped default slot:

Group help text
Loading live example...

Code reference

GlFormGroup

The default slot exposes ariaDescribedby, descriptionId, id, and labelId. These IDs reference the group's rendered elements and can be used to associate custom controls with them.

label-class accepts a string, array, or object of additional label classes. label-size accepts sm or lg to apply the corresponding Bootstrap label-size class; the default Pajamas label styling is recommended.

import { GlFormGroup } from '@gitlab/ui';

Props

Name
Description
Default

id

string Used to set the `id` attribute on the root element, and used as the base to generate any additional element IDs as needed.

undefined

state

boolean Controls the validation state appearance of the component. `true` for valid, `false` for invalid, or `null` for no validation state.

null

description

string Text to place in the help text area of the form group.

undefined

disabled

boolean When `true`, disables all form controls contained in the fieldset. Has no effect when `labelFor` is set.

false

invalidFeedback

string Text to show when the form group has an invalid state.

undefined

label

string Text to place in the label/legend of the form group.

undefined

labelClass

string|array|object Additional CSS class(es) to apply to the label element.

null

labelDescription

string Descriptive text to display below the label.

''

labelFor

string Set to the `id` of a singular form control in the form group. Renders a `label` element instead of a `legend`, and a `div` instead of a `fieldset`.

undefined

labelId

string The `id` to apply to the label/legend element. Generated automatically if not provided.

undefined

labelSize

string Sets the size of the label text. Accepts `sm` or `lg`.

undefined

labelSrOnly

boolean When `true`, visually hides the label/legend while keeping it accessible to screen readers.

false

optional

boolean When `true`, displays optional text next to the label.

false

optionalText

string Text to display when the field is optional.

'(optional)'

validFeedback

string Text to show when the form group has a valid state.

undefined

Slots

Name
Description
label

Content of the label or legend. Replaces the `label` prop and the optional text.

label-description

Content shown below the label. Replaces the `label-description` prop.

default

The form controls of the group.

invalid-feedback

Content of the invalid feedback. Replaces the `invalid-feedback` prop.

valid-feedback

Content of the valid feedback. Replaces the `valid-feedback` prop.

description

Help text shown below the form controls. Replaces the `description` prop.

Last updated at: