Form Field
Label, hint, error, warning and character counter around any form control, wired for assistive technology.
Attributes
| Name | Type | Default | Description |
|---|---|---|---|
label | string | — | Plain-text label. The label slot wins. |
hint | string | — | Plain-text hint shown under the control while there is no error or warning. The hint slot wins. |
error-text | string | — | A custom error. Setting it makes the control invalid (setCustomValidity), so a form blocks submit until it is cleared. |
warning-text | string | — | A non-blocking warning, shown when there is no error. The form still submits. |
counter | boolean | false | Shows length / maxlength under the control when the control has a maxlength. |
necessity | FormFieldNecessity | undefined | undefined | Marker style: required puts an asterisk on required fields, optional puts "Optional" on fields that are not
required, none shows neither. Unset, it follows the enclosing c2-form, then required. |
validate-on | FormFieldValidateOn | undefined | undefined | When an error first shows: submit (default; the control's invalid event or validate()), blur (when focus
leaves an edited control) or input (on every edit). Unset, it follows the enclosing c2-form, then submit. |
Slots
| Name | Description |
|---|---|
default | The control: one form-associated c2-* element or a native input, select or textarea. |
label | Rich label content; replaces label. |
label-action | A link or button at the end of the label row, e.g. "Forgot password?". |
hint | Rich hint content shown under the control; replaces hint. |
error | Error messages, read as text. Give one data-match="valueMissing" (or any other validity key) to show it only for that failure; one without data-match is used for any failure. |
CSS parts
Shadow DOM styling hooks used with ::part(name). Prefer CSS custom properties when they cover the change.
| Name | Description |
|---|---|
label-row | The row holding the label, the necessity marker and the label action. |
label | The label text. |
supporting-text | The line under the control holding the hint, error or warning and the counter. |
counter | The character counter. |
CSS custom properties
Grouped by semantic target and state from --c2-<component>__<target>__<state>--<property>. Click a group to collapse it; click a name to copy it.
| Name | Type | Default | Description |
|---|---|---|---|
--c2-form-field--direction | flex-direction | column | row puts the label beside the control. |
--c2-form-field--gap | pixel | 6px | Space between the label row, the control and the supporting line. |
--c2-form-field--column-gap | pixel | 16px | Space between the label and the control in row direction. |
--c2-form-field--grid-column | grid-column | auto | 1 / -1 spans every column of a c2-form grid. |
--c2-form-field__body--min-width | pixel | 240px | In row direction, the label moves above the control once the control would be narrower than this. |
--c2-form-field__label--width | pixel | auto | Label column width in row direction. |
--c2-form-field__label--padding-top | pixel | 0px | Aligns the label with the control's text in row direction (8px for a 36px control). |
--c2-form-field__label--color | color | #18181b | |
--c2-form-field__label--font-size | font-size | 14px | |
--c2-form-field__label--font-weight | font-weight | 500 | |
--c2-form-field__label--line-height | line-height | 20px | |
--c2-form-field__required-indicator--color | color | #dc2626 | |
--c2-form-field__optional-indicator--color | color | #71717a | |
--c2-form-field__optional-indicator--font-size | font-size | 12px | |
--c2-form-field__label-action--color | color | #0265dc | |
--c2-form-field__label-action--font-size | font-size | 12px | |
--c2-form-field__supporting-text--font-size | font-size | 12px | Hint, error, warning and counter. |
--c2-form-field__supporting-text--line-height | line-height | 16px | |
--c2-form-field__supporting-text--indent | pixel | 0px | Start padding of the supporting line, e.g. to line it up with a checkbox's text. |
--c2-form-field__hint--color | color | #71717a | |
--c2-form-field__error--color | color | #dc2626 | |
--c2-form-field__warning--color | color | #b45309 | |
--c2-form-field__icon--size | pixel | 14px | The error and warning icon. |
--c2-form-field__icon-glyph--color | color | #ffffff | The exclamation mark inside the icon. |
--c2-form-field__counter--color | color | #71717a | |
--c2-form-field__counter__near-limit--color | color | #b45309 | Counter colour once 10% or less of the limit is left. |
Text and languages
The text the component shows, in English. It shows another language when the app adds one with addTranslations, under form-field, and the nearest lang asks for it; any message not translated stays in English. {0}, {1}… stand for the values a message carries. Languages guide.
| Key | English | Values |
|---|---|---|
optional | Optional | — |
errorPrefix | Error: | — |
warningPrefix | Warning: | — |
charactersLeft | {0} characters left | {0} count |
valueMissing | Fill in this field. | — |
valueMissingChoice | Select an option. | — |
valueMissingCheck | Check this box to continue. | — |
typeMismatch | Enter a valid value. | — |
typeMismatchEmail | Enter an email address, like name@example.com. | — |
typeMismatchUrl | Enter a web address, like https://example.com. | — |
patternMismatch | Use the requested format. | — |
tooShort | Use at least {0} characters. | {0} min |
tooLong | Use {0} characters or fewer. | {0} max |
rangeUnderflow | Enter {0} or more. | {0} min |
rangeOverflow | Enter {0} or less. | {0} max |
stepMismatch | Enter a valid value. | — |
badInput | Enter a valid value. | — |