Vanilla Breeze v0.7.4

Vanilla Breeze v0.7.4 released

0.7.4 gives the Ambient pack weather. A page names one feed URL on its own site, and the pack sets data-weather from it, uses its sunrise and sunset for dawn and dusk, and never shows a stale observation. Helpers for producing the feed from Open-Meteo are included. Ambient p…

Read the release notes

required

Marks a form control that must have a value before its form can be submitted. Works without JavaScript; Vanilla Breeze adds the asterisk, the error styling and custom messages.

Overview

required is a boolean attribute that makes a control invalid while it has no value. An invalid control blocks form submission, matches :invalid (and :user-invalid once the user has interacted), and is announced as "required" by screen readers.

Applies to: <input> (every type except hidden, range, color and the button types), <select> and <textarea>.

No asterisk is typed into the label: <form-field> adds it for any field that contains a required control.

Values

required is boolean: present means required, absent means optional. required="false" still makes the field required.

ControlCounts as empty when
Text-like <input>, <textarea>The value is the empty string. Whitespace counts as a value.
<select>No option is selected, or the selected option is the placeholder label option: the first option, with value="", directly in a <select> that has no multiple and a display size of 1. A required select without such an option always has a value.
<select multiple>No option is selected.
CheckboxIt is not checked.
RadioNo radio with the same name in the same form is checked. required on any one radio makes the whole group required.
FileNo file is selected.

Examples

Select with a placeholder option

Radio group

Wrap the group's <fieldset> in one <form-field>. The asterisk goes on the legend, and the field binds every radio that shares the name.

Required checkbox

"At least N" checkboxes

required on a checkbox means that one box must be checked. For "pick at least one of these", put data-min-checked on the fieldset of a data-validate form instead.

In Vanilla Breeze

  • Asterisk. form-field:has(:required) adds a red * after the field's label, or after the legend of a fieldset it wraps (src/custom-elements/form-field/styles.css). It is silent to screen readers, which announce the native required state instead. Never type * into the label text; you would get two.
  • Error and valid styling. :user-invalid or [aria-invalid="true"] gives the field the error border with a thicker inline-start edge (src/native-elements/input/styles.css). Inside <form-field>, a required field that becomes :user-valid gets the success border and its hint collapses.
  • Messages without script. data-message-valuemissing on the control sets the text shown in output.error, in place of the browser's generic "Please fill out this field". The other data-message-* keys are listed on form-field.
  • Timing. <form-field> validates when focus leaves the field, then on every edit once an error has shown. form[data-validate] validates everything on submit, focuses the first error and can fill an error summary.
  • Hidden sections. data-show-when, data-hide-when and data-wizard-if disable every control in a section they hide (marked data-conditional-disabled). A required field in a hidden section therefore neither blocks submit nor sends a value, and it is re-enabled when the section shows again (src/lib/form-conditions.js).
  • Wizard. In a data-wizard form, Next validates the current step's controls (required included), and the final submit jumps back to the step that holds the first invalid field.

Accessibility

  • Screen readers announce native required. Don't add aria-required="true" as well; it is only for custom controls without the native attribute.
  • Show which fields are required before the user submits. The form-field asterisk does that; say what it means once, near the top of long forms ("Fields marked * are required"), or mark the optional fields instead when most are required.
  • Error text must say what to do. Put it in data-message-valuemissing so it appears in the field's live output.error.
  • Don't disable the submit button until the form is valid. A disabled button gives no reason; let the user submit and show the errors.

Gotchas

  • Validity is live; reporting waits. A required field is invalid from the start and becomes valid the moment it has a value, so :invalid matches on page load. The browser only reports the error (bubble, focus) on submit or reportValidity(). Style with :user-invalid, which waits for interaction.
  • disabled and readonly controls are barred from validation, so required on them has no effect.
  • required does nothing on a <select> whose first option has a real value: something is always selected.
  • novalidate on the form, or formnovalidate on the submit button, skips the check on a plain form. A data-validate form checks every submit; see formaction for Save-draft buttons.
  • Whitespace is a value. " " satisfies required; add a pattern such as .*\S.* if that matters.

Related

Which to choose

  • required — one control must have a value.
  • data-min-checked / data-max-checked on a fieldset — a number of checkboxes in a group (needs data-validate).
  • data-show-when — a field is required only in some cases: put it in a conditional section rather than toggling required by hand.
↑