# Slab 1.1.0

> Validation errors from markup, more of the native form, field and popup APIs, and Ember types.

1.1.0 is the first release since 1.0, and its headline is validation errors: every form control can now show one from markup, in your own words, with no script. It fills in more of the platform too. Form controls have the defaults and labels a native input has, and popups and fields gain the methods their native elements have. It's a minor release, so nothing needs changing.

1.1.0 isn't on npm yet. This is what's in it, from `main`, ahead of the release.

## TL;DR

- `custom-validity` and a `validity-message` slot show a validation error, in your own words, from markup.
- Every form control has `defaultValue` or `defaultChecked`, and `labels`, and an outside `<label>` works as it does for a native one.
- `toggle(force)`, `showPicker()`, `stepUp()` and `setRangeText()` are on the components whose native elements have them.
- Ember templates are type-checked with Glint, and every component page shows its usage in Ember.
- Nothing to change: every addition adds to what you have.

## Validation errors, your way

1.0 gave every form control the native validation API: `required`, `pattern`, `setCustomValidity()`, `reportValidity()` and `validationMessage`, with the browser's own wording. All of that still works. But rewording an error, or showing one from your server, took script. 1.1 makes it markup: a `validity-message` slot for your own words, and a `custom-validity` attribute for an error you already know about. They work on every form control, in every framework, and the message is still the control's accessible description.

1.0: script to reword a required field:

```js
const field = document.querySelector('slab-text-field');

const check = () => {
  field.setCustomValidity('');

  if (field.validity.valueMissing) {
    field.setCustomValidity(
      'Give the job a name so the crew can find it.',
    );
  }
};

check();
field.addEventListener('input', check);
```

1.1: markup:

```html
<slab-text-field label="Job name" required>
  <span slot="validity-message">
    Give the job a name so the crew can find it.
  </span>
</slab-text-field>
```

### Your own words

The snippet above, live. Tab into the field and away again, and your message shows in place of the browser's.

```html
<slab-text-field label="Job name" required>
  <span slot="validity-message">
    Give the job a name so the crew can find it.
  </span>
</slab-text-field>
```

### Errors from your server

`custom-validity` is `setCustomValidity()` as an attribute, so a page rendered on a server can say what's wrong. It shows at once, with no script. Set it to an empty string to clear it.

```html
<slab-text-field
  label="Email"
  type="email"
  value="sam@example.com"
  custom-validity="That email already has an account."
></slab-text-field>
```

### Every control

Checkbox, Select and the rest of the form controls take the slot too, so a whole form's errors read the same. Tab through both and away.

```html
<slab-checkbox
  label="I've read the safety notes"
  required
>
  <span slot="validity-message">
    Read the safety notes before you book.
  </span>
</slab-checkbox>

<slab-select
  label="Crew"
  placeholder="Pick a crew"
  required
>
  <slab-option value="north">North</slab-option>
  <slab-option value="south">South</slab-option>
  <span slot="validity-message">
    Pick a crew for this job.
  </span>
</slab-select>
```

### A message for each problem

The slot is markup, so it can hold a link or your page's own language, and you can change it. Pick the words on "invalid", which fires just before the message shows, and `validity` says what's wrong.

```js
const field = document.querySelector('slab-text-field');
const message = field.querySelector('[slot="validity-message"]');

field.addEventListener('invalid', () => {
  message.textContent = field.validity.valueMissing
    ? 'Enter an email so we can send the schedule.'
    : "That doesn't look like an email. Check for typos?";
});
```

[Read the validation guide, in every framework](../components/form.md#validation)

## New features

- [Errors from markup](../components/form.md#validation): `custom-validity` is `setCustomValidity()` as an attribute. The control is invalid with that message, shown right away, for errors you already know about, such as ones from a server.
- [Your own words](../components/form.md#your-own-words): Content in the `validity-message` slot shows in place of the message while the control is invalid, for wording of your own, a translation or a link.
- [Default values](../components/form.md#resetting): Form controls have `defaultValue`, and Checkbox, Switch and Radio have `defaultChecked`, as native inputs do: the value a form resets to.
- [Labels](../components/form.md#labels): Every form control has a read-only `labels`, the `<label>` elements associated with it, from `ElementInternals`.
- [toggle(force)](../components/menu.md): Menu, Context Menu, Select, Combobox, Tooltip and Hover Card have `toggle(force?)`, as Popover does, like a native `togglePopover()`.
- [showPicker()](../components/select.md): Select and Combobox open from `showPicker()`, as Date Picker does, and throw as a native select does outside a user gesture.
- [Stepping a slider](../components/slider.md): Slider and Range Slider have `stepUp()` and `stepDown()`, as a native range input does.
- [Selecting text](../components/text-field.md): Text Field, Textarea, Search Field and Password Field have `selectionStart`, `selectionEnd`, `selectionDirection` and `setRangeText()`, as a native input does.
- [Ember](../get-started.md#typescript): Import `slab-elements/ember.js` once, and Glint checks each tag's attributes by their HTML names. Every component page shows its usage in Ember too.

## Fixes

### Labels

- Clicking an outside `<label>` toggles a Checkbox or Switch, opens File Drop, and focuses every other control, as it does for a native one. In Checkbox, Switch, File Drop, Select, Listbox, Radio Group, Checkbox Group, Rating.

### Validation

- Cancelling "invalid" keeps the validity message unshown, on a blur, a submit and `reportValidity()`, as on a native control. In Form.
- A `setCustomValidity()` message on a control that isn't required stays until it's cleared. In Checkbox, Checkbox Group, Combobox, Listbox, Radio Group, Rating, Select, Switch.

## Upgrade

```sh
pnpm add slab-elements@1.1.0
```
