Field

A label over a control, with help text or an error under it. The control inside is wired up for you: its id, what describes it, and whether it's invalid.

Example

Make it 9 digits and the error goes away.

<Field label="MMSI" description="From the vessel's radio licence." {error}>
	<Input bind:value={mmsi} inputmode="numeric" />
</Field>

Validation

Send it empty. Each field shakes once as its error appears; sending again shakes what's still wrong and puts the cursor in the first.

m
<form bind:this={form} onsubmit={submit} novalidate>
	<Field label="Name" error={errors.name}>…</Field>
</form>

function submit(event) {
	event.preventDefault();
	tried = true;
	// Shakes every invalid field and focuses the first. False when all is well.
	if (shakeInvalid(form)) return;
	save();
}

A form

Rules

  • Label above the control, always. Next to it only in a settings row, where the control is a switch or a short select.
  • Mark the optional fields, not the required ones: most fields are required.
  • Help text says what the label can’t: the format, a limit, who sees it. Leave it out when there’s nothing to add.
  • An error says what’s wrong and how to fix it, and shows after the field is left or the form is sent, not while typing the first character.
  • A failed submit shakes the invalid fields and focuses the first, with shakeInvalid(form). Don’t just disable the submit button: people can’t tell what’s missing.
  • Fields in a form are 24px apart. First and last name can share a row; other fields don’t.