Form Validation Options
import { ValidationGroup, ValidationError } from 'cx/widgets';Form fields indicate invalid input to the user by providing visual feedback such as changing border colors, displaying tooltips or error messages.
By default, inputs will change colors and provide an error tooltip.
If you click on the field below and then click somewhere else, you'll see the change. Furthermore, if you hover over the input with the mouse (or touch it) afterwards, the tooltip will appear as well.
Required fields can be highlighted by setting the asterisk flag. Alternatively,
fields can be marked as visited which will force them to show validation errors immediately.
Form fields provide the help property which can be used to display additional information
next to the field.
help can be used to put any content next to the field. This can be a button or a validation error.
Please note that the second field in the example below will show both the tooltip and the error message.
Sometimes, tooltips are not the best way to indicate errors. Instead of a tooltip,
you may decide to show only the error message. In that case, make sure that all
fields are wrapped inside a ValidationGroup element.
Form fields accept validation callback functions through onValidate.
onValidate can be used for server validation by returning a promise.
<ValidationGroup layout={LabelsTopLayout}>
<TextField
label="Username"
value-bind="$page.form.username"
required visited
onValidate={
v => new Promise(fulfill => {
setTimeout(() => {
fulfill(v == 'cx' ? "This name is taken." : false);
}, 500)
})
}
help={
<div layout={FirstVisibleChildLayout}>
<ValidationError />
<Icon name="check" style="color:green"/>
</div>
}
/>
</ValidationGroup>