Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interface guidelines for forms #261

Merged
merged 38 commits into from
Aug 5, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
5cc8cac
WIP
mperrotti Jan 24, 2022
4c7222d
WIP
mperrotti Feb 24, 2022
e8b570a
Merge branch 'main' of github.com:primer/design into mp/forms-docs
mperrotti Apr 26, 2022
2a62bd5
adds interface guidelines for forms
mperrotti Apr 27, 2022
0f1bc44
Update content/ui-patterns/forms.mdx
mperrotti May 4, 2022
e06b2c6
Update content/ui-patterns/forms.mdx
mperrotti May 4, 2022
3804436
Update content/ui-patterns/forms.mdx
mperrotti May 4, 2022
a202e05
Update content/ui-patterns/forms.mdx
mperrotti May 4, 2022
3c6e364
Update content/ui-patterns/forms.mdx
mperrotti May 4, 2022
a373809
Update content/ui-patterns/forms.mdx
mperrotti May 4, 2022
32f4e6e
Update content/ui-patterns/forms.mdx
mperrotti May 4, 2022
786bdcd
Update content/ui-patterns/forms.mdx
mperrotti May 4, 2022
3a858fe
Update content/ui-patterns/forms.mdx
mperrotti May 4, 2022
cee08d2
Update content/ui-patterns/forms.mdx
mperrotti May 4, 2022
67c15b3
Update content/ui-patterns/forms.mdx
mperrotti May 4, 2022
d6e29a4
Update content/ui-patterns/forms.mdx
mperrotti May 4, 2022
62492d4
Update content/ui-patterns/forms.mdx
mperrotti May 4, 2022
91498ae
Update content/ui-patterns/forms.mdx
mperrotti May 4, 2022
5a26f08
addresses feedback from design infrastructure teammates
mperrotti May 5, 2022
8664bde
removes unnecessary heading
mperrotti May 5, 2022
d5e2d4d
updates action menu example image
mperrotti May 12, 2022
d201897
updates focus styles in supporting graphics
mperrotti May 12, 2022
3cd148e
Update content/ui-patterns/forms.mdx
yaili May 13, 2022
1333064
updates guidance about placeholder text
mperrotti May 13, 2022
a607378
Merge branch 'mp/forms-docs' of github.com:primer/design into mp/form…
mperrotti May 13, 2022
0c55099
adds guidance on error summaries, removes checkbox group validation e…
mperrotti May 17, 2022
5758eba
addresses feedback about unclear guidelines
mperrotti May 17, 2022
b4ff765
Update content/ui-patterns/forms.mdx
mperrotti May 23, 2022
9b6b785
adds guidance about form control sizing
mperrotti May 24, 2022
e1b6e2c
Merge branch 'mp/forms-docs' of github.com:primer/design into mp/form…
mperrotti May 24, 2022
8a4ad2c
shortens intro paragraph, removes guidance about hiding labels
mperrotti May 24, 2022
0d0c6c5
updates example images
mperrotti May 25, 2022
265a168
renders gifs as HTML video elements
mperrotti May 25, 2022
90544e0
adds custom video player
mperrotti May 26, 2022
4e2265b
adds aria labels to icon buttons
mperrotti May 26, 2022
32e8aad
Merge branch 'main' of github.com:primer/design into mp/forms-docs
mperrotti Jun 1, 2022
fe1aa91
Merge branch 'main' of github.com:primer/design into mp/forms-docs
mperrotti Jun 2, 2022
585268e
addresses feedback about formcontrol anatomy diagrams
mperrotti Jun 2, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions content/components/textInputField.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
title: TextInputField
status: Alpha
---
295 changes: 295 additions & 0 deletions content/ui-patterns/forms.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,295 @@
---
title: Forms
mperrotti marked this conversation as resolved.
Show resolved Hide resolved
---

import {Box, Text} from '@primer/components'

<Box sx={{fontSize: 3}} class="lead" as="p">
Forms are used to complete tasks that require data input from the user. For example: creating a new repo, configuring settings, and logging in.
</Box>

<Box sx={{fontSize: 3}} class="lead" as="p">
Primer's form design guidelines aim to minimize the effort and cognitive load required to complete a task that involves a form.
</Box>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mperrotti Can we simplify this leading description and make it into a single paragraph?

Forms are used to complete tasks that require data input from the user. For example: creating a new repo, configuring settings, and logging in.

Primer's form design guidelines aim to minimize the effort and cognitive load required to complete a task that involves a form.


## Form control anatomy

![diagrams labeling the anatomy of a text field and a checkbox field](https://user-images.githubusercontent.com/2313998/164065184-e3c615a3-fb68-4878-b7f8-9a57efb748cb.png )
mperrotti marked this conversation as resolved.
Show resolved Hide resolved

### Label (required)
mperrotti marked this conversation as resolved.
Show resolved Hide resolved

Labels should be descriptive, and as short as possible: ideally no more than 3 words. Labels should be written in sentence case. For example: "Repository name".
mperrotti marked this conversation as resolved.
Show resolved Hide resolved

If you're having trouble keeping label text short, consider using a [caption](#caption) to provide more context.

In rare cases, a label may be hidden when the user has enough context to understand what the input does. When a label is visually hidden, label text must still be provided for assistive technology such as screen readers. For example: a search field with an icon that is placed in the spot a user would expect to find a search field may have a visually hidden label that says "Search".
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In rare cases, a label may be hidden

For example: a search field with an icon that is placed in the spot a user would expect to find a search field may have a visually hidden label that says "Search".

Is a search field part of a forms pattern? Search fields appear throughout dotcom, and they're not rare =).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is a search field part of a forms pattern?

It could be, but usually it isn't. I was trying to think of the most common and obvious example.

Do you (or any other reviewers) have any better examples?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mperrotti I'm not sure. It's confusing to see an example about a search field while this page is specifically about [long-form] forms pattern.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point about these guidelines being for long-form forms. I think it might be best to exclude this paragraph. Visually hidden labels can be addressed in form component guidelines.


Placeholder text is never an acceptable substitute for a form control label because:
mperrotti marked this conversation as resolved.
Show resolved Hide resolved

- The placeholder text disappears as soon as the input has a value
- Placeholder text colors are typically too light to meet the minimum color contrast ratio required to be accessible
mperrotti marked this conversation as resolved.
Show resolved Hide resolved
- Screen readers do not read placeholder text

<DoDontContainer>
<Do>
<img role="presentation" src="https://user-images.githubusercontent.com/2313998/164065187-d300024d-ce39-4f5e-82ad-9e74c4b6d28a.png" />
<Caption>Use a placeholder to hint at what kind of information is expected</Caption>
</Do>
<Dont>
<img role="presentation" src="https://user-images.githubusercontent.com/2313998/164065188-35462873-3706-4063-b7b9-9d0993665307.png" />
<Caption>Don’t use a placeholder as a substitute for a label</Caption>
</Dont>
</DoDontContainer>

### Required field indicator

When a field is required to have a value, it should be visibly marked as required. An individual checkbox or radio cannot be marked as required.
mperrotti marked this conversation as resolved.
Show resolved Hide resolved

For more information, see the [required fields](#required-fields) section.

### Input (required)

The input is what the user interacts with to set the value of the input. You may pre-fill inputs with smart default values, but be careful about making too many assumptions about what a user wants or needs.
mperrotti marked this conversation as resolved.
Show resolved Hide resolved

The input must have a minimum contrast ratio of 3:1 to pass WCAG AA-compliance. This is defined in [WCAG success criterion 1.4.11](https://www.w3.org/WAI/WCAG21/Understanding/non-text-contrast.html).
mperrotti marked this conversation as resolved.
Show resolved Hide resolved

### Caption

A caption may be used to provide additional context about the field to help users fill in the correct data or explain how the data will be used. Caption text should be as short as possible.

Caption text may be displayed with a validation message, or it may be hidden if it is redundant information.
mperrotti marked this conversation as resolved.
Show resolved Hide resolved

Caption text may be used to augment the [label](#label), but should not be redundant with the label or any other parts of the form control. If the caption feels redundant, try removing it.

<DoDontContainer>
<Do>
<img role="presentation" src="https://user-images.githubusercontent.com/2313998/164065190-5ef9d8db-c9ca-4aec-a2ea-321a81cc080e.png" />
mperrotti marked this conversation as resolved.
Show resolved Hide resolved
<Caption>Use caption text that shows new information and provides helpful context</Caption>
</Do>
<Dont>
<img role="presentation" src="https://user-images.githubusercontent.com/2313998/164065192-f1fd1c28-2031-4bb1-836b-607d14a3d4f4.png" />
<Caption>Don’t use caption text that is redundant</Caption>
</Dont>
</DoDontContainer>

### Validation message
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The examples using checkbox groups have some weird application of bold. Do we really want to use bold labels for every checkbox or radio button item? Are the validation messages also meant to be bold? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are all meant to be bold. That's how the components are currently designed.

I'd be happy to change the checkbox and radio labels from bold to regular weight in separate PCSS and PRC PRs. What do you think @vdepizzol?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the intent with the semibold label is that the styles are consistent through all form elements (so input, select etc.) But I don't feel that the validation messages should also be semibold.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The semibold error messages are kind of new: primer/react#2013

@colebemis and I think it helps the text feel more balanced with the leading icon, but I'm open to changing it back to regular weight it if it's causing an issue.

Other reviewers - please chime in if you agree with @langermank that validation text should go back to regular weight.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mperrotti I'd keep it at a regular weight. Validation messages can appear multiple times to the user in the same form. The bolded font feels too heavy.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I'll update the example images and submit a PR to PRC to change the weight back to normal.


A single validation message may be displayed to provide helpful information for a user to complete their task. For example: explain an invalid value and submit the form.
mperrotti marked this conversation as resolved.
Show resolved Hide resolved

An individual checkbox or radio should not have its own validation.
yaili marked this conversation as resolved.
Show resolved Hide resolved

<DoDontContainer>
<Do>
<img role="presentation" src="https://user-images.githubusercontent.com/2313998/164065196-b0146ade-eccb-4a71-b1b6-f4a82b96c115.png" />
mperrotti marked this conversation as resolved.
Show resolved Hide resolved
<Caption>Show validation for the group of inputs</Caption>
mperrotti marked this conversation as resolved.
Show resolved Hide resolved
</Do>
<Dont>
<img role="presentation" src="https://user-images.githubusercontent.com/2313998/164065199-6cfa4893-55cb-4f81-a7d6-4b6ff66339eb.png" />
<Caption>Don’t show validation for each input</Caption>
mperrotti marked this conversation as resolved.
Show resolved Hide resolved
</Dont>
</DoDontContainer>

Information from the caption should not be repeated in the error message. Show the validation message and remove the caption. A validation message makes the field easier to spot when a user is scanning for invalid fields.

<DoDontContainer>
<Do>
<img role="presentation" src="https://user-images.githubusercontent.com/2313998/164065196-b0146ade-eccb-4a71-b1b6-f4a82b96c115.png" />
<Caption>Hide a caption that is redundant with the validation message</Caption>
</Do>
<Dont>
<img role="presentation" src="https://user-images.githubusercontent.com/2313998/164065194-8f123836-5cda-4eea-8f0f-f1c016aa197c.png" />
<Caption>Don’t show a repetitive validation message</Caption>
mperrotti marked this conversation as resolved.
Show resolved Hide resolved
</Dont>
</DoDontContainer>

For more information about form validation, see the [validation guidelines](#invalid-input).

## Input methods
mperrotti marked this conversation as resolved.
Show resolved Hide resolved
mperrotti marked this conversation as resolved.
Show resolved Hide resolved

### Open-ended text

![Text input, open-ended autocomplete text input, plain text input, textarea](https://user-images.githubusercontent.com/2313998/164782311-93a743fb-7757-4f29-bc74-522408a01b5c.png)

Use an open-ended text field when the field does not have a list of possible values. If it's possible to suggest values, use an [Autocomplete](#autocomplete) that allows users to pick a value or enter their own.
mperrotti marked this conversation as resolved.
Show resolved Hide resolved

### A set of selectable options

![Action menu, autocomplete input with limited options, checkbox group, radio group, select](https://user-images.githubusercontent.com/2313998/164782313-851a5a90-8c50-409f-b03d-4200928e0a67.png)

Show a set of selectable options when there is a finite number of possible values.

## Structure

Forms should have a structure that makes it easy for users to scan. Form controls that are stacked vertically and left-aligned are easier for sighted users to scan visually.
mperrotti marked this conversation as resolved.
Show resolved Hide resolved

<DoDontContainer>
<Do>
<img role="presentation" src="https://user-images.githubusercontent.com/2313998/164065202-5c3aba0b-5cef-4ef4-9339-7192a3ebf23a.png" />
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The radio button and checkbox groups seem to have inconsistent headings and unnecessary bold text. Could we revisit these design decisions?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, let's revisit. I think the checkbox and radio labels should be the same weight as body text.

@vdepizzol - could you open an issue or discussion?

<Caption>Default to vertically stacked form controls</Caption>
</Do>
<Dont>
<img role="presentation" src="https://user-images.githubusercontent.com/2313998/164065205-4000430d-2216-4954-a367-86fbd0254cc3.png" />
<Caption>Don’t lay out forms into columns just to reduce the vertical space used by the form</Caption>
</Dont>
</DoDontContainer>
mperrotti marked this conversation as resolved.
Show resolved Hide resolved

### Order of form controls

Form fields should be in a predictable order that flows intuitively.

To achieve an intuitive flow:
- Order fields by their relative level of importance
- Keep related fields near each other
- When possible, keep inputs that require keyboard input near each other so that users who use a mouse don't have to switch back and forth between clicking and typing

### Grouping form controls

<Box mb={3} display="flex" alignItems="flex-start" flexDirection={["column", "column", "column", "column", "row"]} sx={{gap: 3}}>
<img
width="456"
alt="visually grouped of related inputs"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

I'm not sure I understand what this image is telling here —

  • some headings are inside groups, some are not.
  • Is "grouping" specifically referring to horizontal placement?
  • Does Group 1.1 inside Group 1 specific which width it should follow?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some headings are inside groups, some are not.

I think you're referring to the labels in the first three form controls as "headings". None of these are headings, they are input labels.


Is "grouping" specifically referring to horizontal placement?

Horizontal placement is just one of the ways you can visually group two or more inputs.


Does Group 1.1 inside Group 1 specific which width it should follow?

I'm not sure I understand this question.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated the image, but it's still a little "squished" because of the annotations.

src="https://user-images.githubusercontent.com/2313998/164065207-16e51727-55b2-48bc-930c-ad9f0890ae10.png"
mperrotti marked this conversation as resolved.
Show resolved Hide resolved
/>
<Box as="p" mt="0">When there is a collection of closely related fields, they should be labeled and visually grouped together. Effective techniques to visually group a collection of form controls include proximity and whitespace.</Box>
mperrotti marked this conversation as resolved.
Show resolved Hide resolved
</Box>

### Progressively disclosed form controls

<Box mb={3} display="flex" alignItems="flex-start" flexDirection={["column", "column", "column", "column", "row"]} sx={{gap: 3}}>
<img
mperrotti marked this conversation as resolved.
Show resolved Hide resolved
width="456"
alt="form section where controls are hidden until the feature is enabled"
src="https://user-images.githubusercontent.com/2313998/164065212-d4a27d66-cc3a-4e27-8c70-58ff8cc6ea65.png"
/>
<Box as="p" mt="0">To keep forms concise, you may choose to hide or show form controls based on selections the user has made.</Box>
</Box>

#### Nested form controls

Sometimes, progressively disclosed form controls can be visually nested under a parent form control. A common pattern is to use a checkbox or radio “checked” state to decide whether to show a related form control.

<Box mb={3} display="flex" alignItems="flex-start" flexDirection={["column", "column", "column", "column", "row"]} sx={{gap: 3}}>
<img
width="456"
alt="Primary discipline radio buttons, selected 'Other' option reveals a text input"
src="https://user-images.githubusercontent.com/2313998/164065214-0c0bc39f-52e5-4534-9c69-a1f6e4566fa9.png"
/>
<Box as="p" mt="0">If the parent form control provides sufficient visual context, you may visually hide the label. However, you must specify text for a visually hidden label that is accessible to screen readers.</Box>
</Box>

<Box mb={3} display="flex" alignItems="flex-start" flexDirection={["column", "column", "column", "column", "row"]} sx={{gap: 3}}>
<div>
<img
alt="Checkbox that progressively discloses a dropdown with an internal label"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the first time I'm being introduced to the internal label concept, I wonder if this is something we should highlight above, on the Labels section?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is only something we'd do with an Action menu component, so that information will go in that component's guidelines.

src="https://user-images.githubusercontent.com/2313998/164065216-a2702413-5e84-4ec0-b990-b72261df0fb1.png"
/>
<img
alt="Checkbox that progressively discloses a labeled text input"
src="https://user-images.githubusercontent.com/2313998/164065217-7969283a-6dbb-43b6-8f62-e4ab995de246.png"
/>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This UI feels confusing, with the checkbox label and text input label having the same alignment and style. I don't think we should use a search icon in that case, but I'm afraid that's not the only thing that's adding to the confusion.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right - this isn't a great example. I'll come up with a better way to show a labeled input that is "nested" and progressively disclosed.

</div>
<Box as="p" mt="0">If the parent form control does not provide sufficient context, a visible label should be shown. Make sure the label of the nested form control doesn’t clash with the label of its parent form control.</Box>
</Box>

For more information about progressive disclosure, see [Progressive disclosure](/ui-patterns/progressive-disclosure).
mperrotti marked this conversation as resolved.
Show resolved Hide resolved

## Validation

### Use Primer for validation

Use Primer instead of browser-native validation UI. Browser-native validation messages are not accessible to screen readers, and they visually clash with Primer styles.
mperrotti marked this conversation as resolved.
Show resolved Hide resolved

### Validation statuses

<Box mb={3} display="flex" alignItems="flex-start" flexDirection={["column", "column", "column", "column", "row"]} sx={{gap: 3}}>
<Box flexGrow={1} flexBasis="0">
<img
alt="input with forbidden characters"
src="https://user-images.githubusercontent.com/2313998/164065220-30c25ca4-827f-4327-95f4-9be888272813.png"
/>
<Box as="p" mt="0"><strong>Error messages:</strong> An invalid field should always have a message explaining why the value does not pass validation. The message should explain why the value is invalid, and unblock users from completing their task by guiding them to a valid value.</Box>
</Box>

<Box flexGrow={1} flexBasis="0">
<img
alt="valid repo name success"
src="https://user-images.githubusercontent.com/2313998/164065221-7e4f6a21-e9a7-465b-9a86-83c61533fdec.png"
/>
<Box as="p" mt="0"><strong>Success messages:</strong> A success message may be used when a user might need extra assurance that the field's value is valid. For example: when creating a repository name, it’s nice to be assured that the name is available and valid.</Box>
</Box>
</Box>

### Validation on submit

The default behavior of the web is to perform validation when the user attempts to submit the form. This lets the user flow quickly through the form without interruption, but the user has to wait until submitting the form to know if they’ve entered invalid data.
mperrotti marked this conversation as resolved.
Show resolved Hide resolved

When the form fails validation, the first invalid input should be focused and scrolled into the viewport.

After a form has failed validation, you may switch to inline validation to provide quicker feedback than if you made the user re-submit the form to re-check validation statuses.
mperrotti marked this conversation as resolved.
Show resolved Hide resolved

### Inline validation
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mperrotti I'm finding it quite confusing to see these 3 gifs next to each other. There's a lot going on 😬 . I'm not sure I have any recommendations for now, but it's something we should pay more attention...

Screen.Recording.2022-05-23.at.10.49.45.mov

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right - all this motion is overwhelming. I'm going to try converting these to <video> elements that can be played or paused individually.


Inline validation provides immediate feedback as the user is entering data. For example: when setting a username, you could show a success message if the name is available or an error message if it’s taken.
mperrotti marked this conversation as resolved.
Show resolved Hide resolved

<Box mb={3} display="flex" alignItems="flex-start" flexDirection={["column", "column", "column", "column", "row"]} sx={{gap: 3}}>
<img
width="456"
alt="screen recording of validation being performed after the user changes and blurs"
src="https://user-images.githubusercontent.com/2313998/164065853-fd7ef55a-7082-4b0d-aa6b-2b25af4c664b.gif"
/>
<Box as="p" mt="0">If the form control is in a valid state, validation should be performed until after the user has made a change to the input <strong>and has removed focus from the input</strong>.</Box>
</Box>

<Box mb={3} display="flex" alignItems="flex-start" flexDirection={["column", "column", "column", "column", "row"]} sx={{gap: 3}}>
<img
width="456"
alt="screen recording of validation being performed as the user tries to fix an invalid input"
src="https://user-images.githubusercontent.com/2313998/164065860-25e23fa0-e22a-435d-adbd-5666a9fd7890.gif"
/>
<Box as="p" mt="0">We shouldn’t attempt to validate an input before the user is done with it. So validation may be performed as the user is typing or making their selection, but only <strong>after the first time the input has been validated</strong> and the <strong>input is in an invalid state</strong>. This gives the user early positive feedback by removing the error if the user makes a change that fixes it.</Box>
mperrotti marked this conversation as resolved.
Show resolved Hide resolved
</Box>

<Box mb={3} display="flex" alignItems="flex-start" flexDirection={["column", "column", "column", "column", "row"]} sx={{gap: 3}}>
<img
width="456"
alt="screen recording of valid repo name input showing loading indicator after blur"
src="https://user-images.githubusercontent.com/2313998/164065857-573ca6db-f2c7-49ac-a3cf-bf479a40973d.gif"
/>
<Box as="p" mt="0">If the form control’s validation is likely to take more than 1 second, show a loading indicator.</Box>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the form control’s validation is likely to take more than 1 second

Is this the right metric to decide whether to display a loading indicator? What if the process is known as a quick one but the user is on a slow connection?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, if we're extending the height of the input control based on an async request that may take long to process, how do we avoid the content to reflow unexpectedly?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the right metric to decide whether to display a loading indicator? What if the process is known as a quick one but the user is on a slow connection?

It shouldn't matter what connection the user is on. This is a detail that would be handled in the implementation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, if we're extending the height of the input control based on an async request that may take long to process, how do we avoid the content to reflow unexpectedly?

That's a great question, and I have no idea how we'd avoid reflow in that case.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the right metric to decide whether to display a loading indicator? What if the process is known as a quick one but the user is on a slow connection?

It shouldn't matter what connection the user is on. This is a detail that would be handled in the implementation.

@mperrotti if there's a server request, wouldn't it make more sense to show the loading spinner regardless? I don't understand how that'd be handled in the implementation 🤔 .

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if there's a server request, wouldn't it make more sense to show the loading spinner regardless?

@vdepizzol - I was trying to avoid showing too many loading indicators because they could make the app feel slower. If we wait 1 second, we're still giving the user feedback that something is happening, but only if it's something that's making the user wait a noticeable amount of time.

Does that make sense?

</Box>

<Note variant="warning">
mperrotti marked this conversation as resolved.
Show resolved Hide resolved
Inline validation offers immediate feedback, but carefully weigh that with the following drawbacks.
mperrotti marked this conversation as resolved.
Show resolved Hide resolved

<ul>
<li><strong>Negative impact on perceived performance when validating server-side:</strong> Inline validation works best with data that can be checked instantly with client-side validation. Using server-side validation for inline validation could make the app feel slow because the user has to wait for the server to send back a validation response.</li>

<li><strong>Interrupted flow for screen reader users:</strong> Inline validation isn’t as disruptive for sighted users because they can choose to ignore the errors until they’re ready to fix them. When a screen reader user moves focus from the invalid input to the next form control, they will be interrupted by the validation message of the previous form control.</li>
</ul>
</Note>

### Validating a group of form controls

<Box mb={3} display="flex" alignItems="flex-start" flexDirection={["column", "column", "column", "column", "row"]} sx={{gap: 3}}>
<img
width="456"
alt="checkbox group with an error"
src="https://user-images.githubusercontent.com/2313998/164065224-d1ba56ce-c632-4318-b9b2-b75940f771a6.png"
/>
<Box as="p" mt="0">A validation message may be used to describe an error for a group of form controls. For example: if a user checks 3 checkboxes when they’re only allowed to check 2 options.</Box>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think my comment above about grouping error messages could be described in this section instead. The case you mention here is a single error message for a group of controls, but we should document how to present multiple individual error messages for a group of controls as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@langermank - if you have an example of multiple individual error messages for a group of controls, that might be helpful.

@alliethu - I remember you saying it's not good to show multiple error messages for a single input, but what about multiple error messages that apply to a group of checkboxes or radios?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's a screenshot from a previous form I worked on:

Contact form with multiple fields showing an error state. At the top of the form, there's a group with a title and list of links to each field, stating what the error is with the field

I think the idea here is if there are multiple errors in a single <form>, show them in a labeled group with links to each field. My original comment might have sounded like multiple errors per field, but that's not what I mean 😅 that feels problematic in a different way!

@alliethu any thoughts here? I pulled this from a book on accessible forms, and that was a year ago. I haven't researched this recently.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@langermank Your annotation is very much inline with what I would have done myself. I'm attaching a rough annotation below that includes a few more details:

Error summary notation

Considerations to make:

  • add role="alert" to the summary div
  • move keyboard focus to the error summary
  • use a semantic heading (I'm thinking an h2 to title the error summary area)
  • reference the heading in the summary div by using aria-labelledby
  • use a semantic list for the error summaries
  • when a link is activated, place focus in its corresponding input field
  • the error messages should be worded the same in the error summary and inline with each input.
    cc/ @mperrotti

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you both for the helpful examples! I misunderstood what was being suggested. I thought @langermank was suggesting this pattern for a single group of checkbox or radio inputs.

It feels aggressive to always show the error summary alert banner. We should explain when a summary is needed and when it isn't. Maybe when there are 3 or more errors?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm noticing repetitive error messages in @alliethu's example, and that feels like it could be overwhelming or confusing to sighted and non-sighted users.

Copy link
Contributor Author

@mperrotti mperrotti May 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I pushed a commit to add some guidance on designing error summaries. I'm not feeling great about it yet, and I'd appreciate some time pairing with somebody from the accessibility team.

Preview deployment

</Box>

## Submission

Forms should follow consistent patterns for [submitting and saving data](/ui-patterns/saving).

## Related components

- Action menu ([React](https://primer.style/react/ActionMenu))
- Autocomplete ([React](https://primer.style/react/Autocomplete))
- Checkbox group ([React](https://primer.style/react/CheckboxGroup))
- Form control ([React](https://primer.style/react/FormControl))
- Radio group ([React](https://primer.style/react/RadioGroup))
- Select ([React](https://primer.style/react/Select))
- Textarea ([React](https://primer.style/react/Textarea))
- Text input ([React](https://primer.style/react/TextInput))
2 changes: 2 additions & 0 deletions src/@primer/gatsby-theme-doctocat/nav.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
url: /ui-patterns/button-usage
- title: Empty states
url: /ui-patterns/empty-states
- title: Forms
url: /ui-patterns/forms
mperrotti marked this conversation as resolved.
Show resolved Hide resolved
- title: Feature onboarding
url: /ui-patterns/feature-onboarding
- title: Messaging
Expand Down