Layout and Validation

Guidance on form field layout and validation display.

Basic Form Layout

As can be explored in the various Form component demos, the <TextField> component is the most versatile.

Combined with the <Grid> component for layout and a standard <form> tag, you can compose your form.

In the following demo, Grid is used as a container to position the name fields horizontally. This is followed by the <FormControl> component which is defining its rendering component as "fieldset" and then wrapping a second Grid container to align the Address fields. <Typography> is used with "legend" defined as the component to label the fieldset, and the visual variant of h3 selected as the legend's type style.

Finally, a submit <Button> is added in a third Grid container to aid in alignment by setting justify="flex-end" on the Grid.

Each field present in this demo also has the required native HTML attribute set, which will cause an additional prompt on blur of the field in some browsers that tells the user to provide a value.

Form Validation

The following creates a simple series of validation functions, but you are encouraged to select a third-party validation service as appropriate for your application needs. In the example, the field-level error state is triggered onBlur of the field and on submit of the form.

When creating a form with the possibility of multiple errors, you may use the Notification component to help highlight the existence of errors.

As a best practice, particularly for accessibility, when the Notification is made visible it should receive focus, as shown, inclusive of continuing to re-assign focus if the form is re-submitted and still contains errors.

Also for accessibility, it is ideal to include a list of links to fields with errors as demonstrated. For these links, you may also use the Unifi Link component with the color prop set to error and underline="always".