Components
Validation message
Validation message is used to display feedback related to the validation of user input, such as error messages or warnings in forms.
<p class="ds-validation-message" data-field="validation">This is a validation message.</p>const PreviewEn = () => { return <ValidationMessage>This is a validation message.</ValidationMessage>; }; render(<PreviewEn />)
Usage
Use the class name ds-validation-message to style a validation message.
Form fields and status
A validation message will automatically be connected to a form field when it has the attribute data-field="validation" and is inside a <ds-field>.
The form element automatically gets aria-invalid="true" when a validation message is visible in the default color danger.
Note: Use the hidden attribute or add/remove the validation message element, rather than hiding/showing with CSS class names or inline styling. This ensures <ds-field> can correctly convey the visibility of the validation message to assistive technologies.
CSS variables and data attributes
Sizes are controlled with data-size and colors with data-color. The component will inherit from the closest parent where these are set.
| Name | Value |
|---|---|
| --dsc-validation-message-color | var(--ds-color-danger-text-subtle) |
| --dsc-validation-message-icon-size | var(--ds-icon-size) |
| --dsc-validation-message-icon-url | url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath fill-rule='evenodd' d='M7.74 2.47a.75.75 0 0 1 .53-.22h7.46a.75.75 0 0 1 .53.22l5.27 5.27c.14.14.22.33.22.53v7.46a.75.75 0 0 1-.22.53l-5.27 5.27a.75.75 0 0 1-.53.22H8.27a.75.75 0 0 1-.53-.22l-5.27-5.27a.75.75 0 0 1-.22-.53V8.27a.75.75 0 0 1 .22-.53zm1.29 5.5a.75.75 0 0 0-1.06 1.06L10.94 12l-2.97 2.97a.75.75 0 1 0 1.06 1.06L12 13.06l2.97 2.97a.75.75 0 1 0 1.06-1.06L13.06 12l2.97-2.97a.75.75 0 0 0-1.06-1.06L12 10.94z'/%3E%3C/svg%3E") |
| --dsc-validation-message-spacing | var(--ds-size-2) |
No relevant data attributes found.
Example
React
import { ValidationMessage } from '@digdir/designsystemet-react';
<ValidationMessage>This is a validation message.</ValidationMessage>
ValidationMessage
- asChild
- Description
Change the default rendered element for the one passed as a child, merging their props and behavior.
- Type
boolean- Default
false
| Name | Type | Default | Description |
|---|---|---|---|
| asChild | boolean | false | Change the default rendered element for the one passed as a child, merging their props and behavior. |