Skip to content

Field

A form field with a label, an input or control, and an optional validation message.

Guidelines

When to use fields

Field provides features for building an accessible, understandable form field to collect user input.

Use the Field component to create form layouts and to create a variety of form inputs, such as text inputs, selects, radio buttons, checkboxes or toggle switches.

If you do not need to collect user input, do not use Field. For example, Field is not necessary to display a search input. Although search is built with a text input component, it triggers an action and is not a form item.

Specifications

Specification of Field.

It is composed of the following elements:

  1. Label
    A label serves as a precise and informative title for the form field, indicating the type of information to be entered. Explore Label to check its different properties.
  2. Label’s description (optional)
    An optional description can also be incorporated below the label to provide additional information about it. This description can include plain text, with or without links.
  3. Field
    The input element, or a group of inputs, where the user enters information. Field can use any form item such as text input, select, checkbox, etc.
  4. Helper text (optional)
    Text that provides supplementary information or instructions to guide users in completing the field correctly. The helper text can include plain text with or without links, and it can also include a number to optionally display the character counter if needed.
  5. Validation message
    An inline validation message will appear to provide feedback about the status of the field. For example, to provide an error message to the user when the field contains one or more errors.

Component limitations

Both label and description will wrap onto multiple lines as needed. There are no strict length limits for labels and descriptions, but it's recommended to keep them concise for improved form readability and scanning.

When a fieldset is arranged horizontally, labels stay aligned, with field elements stacked below respecting Field component paddings.

If a Field includes both helper text and a character counter, they will be aligned at the top with a spacing-100 token (equivalent to 16px in the default Codex theme).

Maximum example of a fieldset with two fields arranged horizontally.

Refer to the Field component in Codex Figma.

Types

The Field component's control can be any form item component designed to gather user input, including:

  • Text input and text area
  • Select
  • Combobox
  • Checkbox, radio groups, and toggle switch
  • Chip input
  • Lookup

Types of Fields based on their content: with a text
input, with a select, with a combobox, with a checkbox group, with a radio group, and with a
toggle switch group.

However, components not intended for collecting user input, such as SearchInput, should not be used within Field.

Interaction states

Form fields have the following visually separate states:

Interaction states of the Field component: default, interaction within field, disabled, error, warning, and success.

  1. Default
  2. Interactive state within the field
  3. Disabled
  4. Error
  5. Warning
  6. Success

The interactive states will exclusively impact the field item within Field. So hover, focus, filled, and read-only states apply solely to the field item (e.g. the text input).

Best practices

Consider the following recommendations when using fields. For more detailed information about form fields, consult the guidelines for constructing forms.

Icon

Field using an start icon next to its label.

Do:
  • Use a start icon next to the label to emphasize the required input in the form field.

Field featuring two icons: one positioned next to the label and the other inside the TextInput.

Don't:
  • Use another icon in the label's section if the field already contains an icon.

Label, description, and helper text

Field with a short label and a lengthy description.

Do:
  • Keep the Field’s label short, clear, and easy to scan.
  • Use the label’s description for additional information related with the label if needed.
  • Use the helper text to provide supplementary guidance on completing the field correctly, including expected input and error prevention.

Field with a lengthy label.

Don't:
  • Make the Field’s label text excessively large, which could make it difficult to scan.
  • Include field completion guidelines in the label’s description.
  • Place information related to the label below the field. Use the helper text for this purpose instead.

Optional form fields

Two Fields: one featuring the "(optional)" indicator.

Do:
  • Only indicate the optional Fields in the form.
  • Ensure that the entire word "optional" is displayed for translation purposes.

Two Fields: one required, marked with an asterisk, and an optional one indicated with "(opt)".

Don't:
  • Mark required labels with an asterisk.
  • Use abbreviations in the optional indicator.

Label style

Fieldset with each Field labeled as regular.

Do:
  • Include multiple Fields within a fieldset for organization.
  • Customize the label of Fields to regular weight when grouped within a fieldset.

Fieldset with each Field labeled as bold.

Don't:
  • Avoid using bold labels in Fields within a fieldset to maintain hierarchy and clarity.

Characters counter

Field including a TextArea with a character counter.

Do:
  • Use a character counter only when it's necessary to limit the number of characters in a form input.
  • Display an inline error message if the character limit is exceeded.

Field including a TextInput with a character counter.

Don't:
  • Use a character counters when the character limit does not need to be communicated to the user.

Content

Form elements should guide the user to fill out the form easily. They will prevent the user from making mistakes.

Label and placeholder

Labels indicate what the input should be. Placeholders act as sample text for the kind of content the user needs to input. Different browsers handle placeholder text differently, and some have trouble displaying translations of placeholders. Use placeholders as helpful additions, but do not rely on them too heavily.

Inputs conveying an example of helpful labels and placeholder text.

Do:
  • Make the label short, clear, and easy to scan. Consistent & Clear
  • Provide more context in the placeholder for what's indicated in the label. Clear
  • Use examples in the placeholder when expected input formats need to be clarified. Clear

Inputs conveying an example of repetitive and redundant labels and placeholder text.

Don't:
  • Leave out the label in favor of just a placeholder. Needed & Translatable
  • Repeat in the placeholder what’s in the label. Concise

Helper text

Helper text gives a user guidance on how to fill in the form field. These messages often clarify formatting or indicate character restrictions. Giving users appropriate context helps them to quickly and efficiently enter the input without errors.

Inputs conveying an example of helpful and concise helper text.

Do:

Inputs conveying an example of unnecessarily long helper text.

Don't:
  • List too many restrictions or use overly technical and robotic language. Relevant

Inline error

These are short, simple contextual messages that allow the user to quickly understand how to fix the error they just made.

Inputs conveying an example of helpful and useful inline error text.

Do:

Inputs conveying an example of inline error text without any instruction to correct the error.

Don't:

Keyboard navigation

KeyFunction
TabIt moves the focus to the next interactive element in tab order.
Shift + TabIt moves the focus to the previous interactive element.
EnterIf the focus is placed on one of the interactive elements within the Field, it activates them.

Demos

Configurable

Note that this configurable demo is only shown with a TextInput inside the Field. See the demos below for use of the Field component with other types of inputs or groups of inputs, along with code samples.

See the validation messages demos below for more information about how to add and customize an error message.

Short description text
Longer help text to explain how to use this field
NameValue
Props
labelIcon
optional
hideLabel
isFieldset
disabled
status
Slots
label
description
help-text
View
Reading direction
Note: For icon properties, the relevant icon also needs to be imported from the @wikimedia/codex-icons package. See the usage documentation for more information.

With rich description and help text

You can include markup in the description and help-text slots. Only links and text formatting are recommended as description and help text should be concise.

With validation messages

You can display a validation message based on the current status of the field. Set the status prop based on the field's validity, then pass in an object of messages keyed on validation status type. If there is a message for the current status, it will be displayed.

The status you bind to the Field component will also be passed down to its child components, which will display appropriate styles for that status if they exist (e.g. a red border in the error state). You do not need to bind the status prop to the child input components. Note that form input components currently only have special styles for the error status.

Setting the status based on field validity is up to you. In the error example below, it's done as you're changing the input. You could also validate on blur to give the user a chance to finish filling out the field, as demonstrated in the warning example and the success example.

Error

The error status can be used both to show an error message and to display the error state of the form input. Try entering a username into the field below that's longer than a single character to see the error state and error message.

Enter a unique username
Username cannot be longer than 1 character

Warning

The following example shows a warning message on blur if the username doesn't meet the criteria written in the help text. In this case, the username will be corrected to meet the criteria, and the message informs the user of this change. Note that form inputs do not display a "warning" state.

Enter a unique username
Username must be capitalized and have no spaces

Success

The following example shows a success message on blur when the username is unique. Note that form inputs do not display a "success" state.

Enter a unique username

Fieldset with radio group

For any field that contains multiple inputs or controls, set the isFieldset prop to true. This will output a <fieldset> element with a <legend> instead of a label.

Groups of Radio or Checkbox components are considered fieldsets.

Notifications Choose how often you'd like to receive notifications
Note that you can update this preference later

Complex field with two inputs

The field below has two inputs, a TextInput and a Select. In addition to the field-level label, each input needs its own individual label. In this case, the individual labels can be visually hidden. In the example below, the labels are included in two different ways:

  • For the TextInput, the Label component is used with hideLabel set to true
  • For the Select, an aria-label is applied

To give the user a chance to type a valid coordinate location, this example doesn't validate as the user types, but delays validation until the input is blurred. For an example of how to do validation as the user types, see the validation message demos above.

Note that, when you enter erroneous data in the TextInput below, error styles for both the TextInput and the Select will display since they are both contained in a single Field. If you need to show separate states for each input, see the nested Fields example below.

Coordinate location
Please enter coordinates as decimal degrees.

Fieldset with nested Fields

In this example, each input within the field needs its own visible label, description, validation status, and validation message. To accomplish this, each input is wrapped in a Field component. The entire field is wrapped in a top-level Field component with isFieldset set to true.

Note that each sub-field has its own status. This enables you to show error styles only for the input that contains the error, not the entire fieldset.

Nested fields will become disabled when their parent field is disabled.

Item weight
Numerical value of the weight
Must be from pre-approved list of units
Hint: try searching for "grams"

Field with custom help text content

The help-text slot is not limited to static text – more complex markup, including other components and bound values, can be provided here as well. Below is an example of a custom character counter embedded inside a Field. The counter is bound to the modelValue of the TextArea component inside the Field, and an error message is displayed if this text exceeds the maximum allowed number of characters.

Please enter a message of 100 characters or less

100

Vue usage

This component can wrap the following:

  • A single form input or control
  • An input group (e.g. a group of Radios or Checkboxes)
  • A set of nested fields (inputs wrapped in their own Field components).

The following Codex components can be used inside the Field component:

  • Checkbox
  • ChipInput
  • Combobox
  • Lookup
  • Radio
  • SearchInput
  • Select
  • TextArea
  • TextInput
  • ToggleSwitch

Props

Prop nameDescriptionTypeDefault
labelIconIcon before the label text.

Do not use this if including a start icon within the input component.
Icon''
optionalWhether the field is optional.

This will add a flag next to the label indicating that the field is optional.
booleanfalse
optionalFlagText to indicate that the field is optional.

Omit this prop to use the default value, "(optional)".
string''
hideLabelWhether the label should be visually hidden.

Note that this will also hide the description.
booleanfalse
isFieldsetWhether this field contains a group of inputs.

When true, this outputs a <fieldset> element with a semantic <legend>. When false, it outputs a <div> with a semantic <label>.
booleanfalse
disabledWhether the entire field is disabled.booleanfalse
statusstatus attribute of the input. This also determines which validation message is shown.ValidationStatusType'default'
messagesMessage text keyed on validation status type.ValidationMessages{}

Slots

NameDescriptionBindings
labelLabel text.
descriptionShort description text.
defaultInput, control, or input group.
help-textFurther explanation of how to use this field.
warningWarning message content for messages containing HTML markup.
errorError message content for messages containing HTML markup.
successSuccess message content for messages containing HTML markup.

CSS-only version

See the CSS-only Label docs for instructions on making the label visually-hidden or adding a label icon.

Markup structure

Single input

Short description text

Fieldset

Inside a <form>, use a <fieldset> element for input groups.

When outputting a <fieldset>, the markup of this component is quite different:

  • The outer element is the <fieldset> instead of a <div>
  • A <legend> is used instead of a <label>. See docs on the CSS-only Label for more info.
  • The description is included inside the <legend>
  • The for and aria-describedby attributes are not needed
Legend text (optional) Short description text

With help text

To add help text below the input or control, add a <div> below the control wrapper with the class cdx-field__help-text

This works with single fields and fieldsets.

Disabled

To display a disabled field:

  • Add the .cdx-field--disabled class to the outer element
  • Add the .cdx-label--disabled class to the .cdx-label element
  • Disable the input(s) or control(s) (in the example below, the disabled attribute is added to the <input> element)

This works with single fields and fieldsets.

Error status and message

To display error styles and show an error message:

  • Apply error styles to the input (in the example below, the .cdx-text-input--status-error class is applied to the text input wrapper)
  • Add the validation message below the control wrapper