In this document, "form field" refers to the wrapper component
"form field control" refers to the component that the
sbb-form-field is wrapping
(e.g., the input, select, etc.)
The following components are designed to work inside a
Either use a
<label> or the
label attribute to provide a label for a form input. The
sbb-form-field will automatically assign the correct id reference between label and input.
It's possible to use the
floatingLabel property to display the label inside the input.
When using it and setting the value programmatically to empty or from empty to a specific value,
it's mandatory to call the
reset() method of the
sbb-form-field to update the state of the floating label.
<sbb-form-field label="Example" floating-label>
<input required />
<sbb-form-error>This field is required!</sbb-form-error>
Error messages can be shown under the form field by adding
sbb-form-error elements inside the form field.
The component will automatically assign them to the
In order to avoid the layout from "jumping" when an error is shown, the option of setting
sbb-form-field will reserve space for a single line of an error message.
It is possible to add content as a prefix or suffix in a
This can be done via the
Some components, like the sbb-form-field-clear or the
sbb-slider, when used within the form field, will automatically occupy
one or both of these slots.
Please refer to their documentation for more details.
<sbb-icon slot="prefix" name="pie-small"></sbb-icon>
<sbb-icon slot="suffix" name="circle-information-small"></sbb-icon>
By default, the component has a defined width and min-width. However, this behavior can be overridden by setting
width property to
collapse: in this way the component adapts its width to the inner slotted input component.
This is useful, for example, for the sbb-time-input component.
However, as the width-styles are exposed to the host,
it's possible to apply any desired width by setting just the
min-width CSS properties.
<input value="13:30" />
By itself, the
sbb-form-field does not apply any additional accessibility treatment to a form
element. However, several of the form field's optional features interact with the form element
contained within the form field.
When you provide a label via
<label> or the
label attribute, the
associates this label with the field's form element via a native
<label> element, using the
attribute to reference the control's ID.
When using a non-native form element (e.g.
aria-labelledby is used to connect the
form element with the label, by setting an id on the label and referencing this id in the
aria-labelledby attribute placed on the form element.
Please note that only one
<label> element is supported. Additionally, if you place the
element outside the
sbb-form-field, the automatic assignment is skipped, and it is up to the
consumer to use the correct id references.
When you provide informational text via
sbb-form-error, it automatically adds these elements' IDs
to the form element's
sbb-form-error is slotted to an element having
aria-live="polite" so that assistive
technology will announce errors when they appear.
| || ||public|| || ||Whether to reserve space for an error message. |
| || ||public|| ||Label text for the input which is internally rendered as |
| || ||public|| ||Indicates whether the input is optional.|
| || ||public|| || ||Size variant, either l or m.|
| || ||public|| || ||Whether to display the form field without a border.|
| || ||public|| || ||Defines the width of the component: - |
| || ||public|| || ||Whether the label should float. If activated, the placeholder of the input is hidden.|
| || ||public|| || ||Negative coloring variant flag.|
| ||-||public|| ||Returns the input element.|
| ||public||Manually reset the form field. Currently, this only resets the floating label.|| |
| ||public||Manually clears the input value. It only works for inputs, selects are not supported.|| |
| ||public||Returns the input element.|| |
|Use this slot to render an input/select or a supported non-native element.|
| ||Use this slot to render a label.|
| ||Use this slot to render an icon on the left side of the input.|
| ||Use this slot to render an icon on the right side of the input.|
| ||Use this slot to render an error.|