<!--
  GENERATED FILE - do not edit by hand.
  Source of truth: src/components/mds-input-field/usage/ in the magma monorepo.
-->

# mds-input-field - mistakes

> The API of this component: [`AGENTS.md`](AGENTS.md).

Common incorrect uses of `<mds-input-field>`. Each entry pairs the wrong form with the right one and a one-line reason. System-wide rules (boolean-as-string, shadow piercing, Tailwind color utilities, raw native event listening) live in [`agents/mistakes.md`](../../../../agents/mistakes.md) - they apply here too but are not repeated.

#### Do Not Use It Without a Slotted Input

`<mds-input-field>` requires a slotted input element. With an empty slot it only logs a console warning at load and has no control to label or validate. Always slot an [`mds-input`](../mds-input/AGENTS.md) as the first child.

```html
<!-- INCORRECT -->
<mds-input-field label="Nome"></mds-input-field>

<!-- CORRECT -->
<mds-input-field label="Nome">
  <mds-input name="firstName" placeholder="Es: Mario"></mds-input>
</mds-input-field>
```

#### Do Not Use a Native `<input>` in the Slot

The component binds to `mdsInputValidation`, which only the Magma inputs (`<mds-input>`, `<mds-input-date>`) emit. Slotting a raw `<input>` skips the validation lifecycle entirely - the field will never switch to `error` or `success`.

```html
<!-- INCORRECT -->
<mds-input-field label="Email">
  <input type="email" name="email" required>
</mds-input-field>

<!-- CORRECT -->
<mds-input-field label="Email">
  <mds-input type="email" name="email" required></mds-input>
</mds-input-field>
```

#### Do Not Manage `variant` and `message` Manually During Validation

`variant` and `message` are mutable and will be overwritten by the validation cycle the moment the slotted control emits `mdsInputValidation`. Trying to manage them via script in parallel leads to race conditions. Set validation attributes on `<mds-input>` and let the field drive state automatically.

```html
<!-- INCORRECT -->
<mds-input-field id="nameField" label="Nome">
  <mds-input name="firstName" required></mds-input>
</mds-input-field>
<script>
  document.getElementById('nameField').variant = 'error';
  document.getElementById('nameField').message = 'Campo obbligatorio';
</script>

<!-- CORRECT -->
<mds-input-field label="Nome">
  <mds-input name="firstName" required></mds-input>
</mds-input-field>
```

#### Do Not Listen for Native `change` or `input` Events to Detect Validation

Native DOM events (`change`, `input`) do not carry validation state and may not bubble out of shadow DOM as expected. Listen for the documented `mdsInputValidation` event on the slotted `<mds-input>` if you need to react to validation results in your own code.

```html
<!-- INCORRECT -->
<mds-input-field label="Codice fiscale">
  <mds-input id="cfInput" name="cf" type="cf" required></mds-input>
</mds-input-field>
<script>
  document.getElementById('cfInput').addEventListener('change', handleChange);
</script>

<!-- CORRECT -->
<mds-input-field label="Codice fiscale">
  <mds-input id="cfInput" name="cf" type="cf" required></mds-input>
</mds-input-field>
<script>
  document.getElementById('cfInput').addEventListener('mdsInputValidation', handleValidation);
</script>
```

#### Do Not Apply an Invalid `variant` Value

`<mds-input-field>` uses `ThemeInputVariantType`, which only accepts `'ai'`, `'primary'`, `'error'`, `'info'`, `'success'`, and `'warning'`. Values from the broader variant ladder (e.g. `'secondary'`, `'dark'`) silently fall back to the default and produce no coloring change.

```html
<!-- INCORRECT (variant from the full theme ladder) -->
<mds-input-field label="Stato" variant="secondary" message="Valore non valido">
  <mds-input name="stato"></mds-input>
</mds-input-field>

<!-- CORRECT -->
<mds-input-field label="Stato" variant="warning" message="Valore non valido">
  <mds-input name="stato"></mds-input>
</mds-input-field>
```

#### Do Not Narrow a Short Field by Hand

A width written on the field, or a wrapper per pair, gives every field an edge of its own and copies the threshold under which the halves take the whole column. `short` in a `field-group` keeps two widths, the whole column and half of it, and the threshold in one place.

```html
<!-- INCORRECT -->
<div class="flex flex-wrap gap-between-fields">
  <mds-input-field label="Numero civico" class="w-32">
    <mds-input name="number"></mds-input>
  </mds-input-field>
  <mds-input-field label="CAP" class="w-1/2">
    <mds-input name="postcode"></mds-input>
  </mds-input-field>
</div>

<!-- CORRECT -->
<fieldset class="field-group">
  <legend>Residenza</legend>
  <mds-input-field label="Numero civico" short>
    <mds-input name="number"></mds-input>
  </mds-input-field>
  <mds-input-field label="CAP" short>
    <mds-input name="postcode"></mds-input>
  </mds-input-field>
</fieldset>
```

#### Do Not Tell the State with the Icon Alone

`icon` is decorative: hidden from assistive technologies, it only lets the eye find a state the message says in words. A field that shows a warning icon with no message tells a screen reader nothing; a symbol written into `label` instead reaches the name of the control, read before its label.

```html
<!-- INCORRECT -->
<mds-input-field label="Spazio colore" variant="warning" icon="mi/baseline/warning">
  <mds-input-select>...</mds-input-select>
</mds-input-field>

<!-- CORRECT -->
<mds-input-field
  label="Spazio colore"
  variant="warning"
  icon="mi/baseline/warning"
  message="La tonalità ruota di 25 gradi nei gradini scuri"
>
  <mds-input-select>...</mds-input-select>
</mds-input-field>
```

#### Do Not Pierce the Shadow DOM to Style the Message Area

The only supported customization surface is `--mds-input-field-message-color` and `--mds-input-field-message-background`, plus the `content` shadow part. Targeting internal elements via `>>>` or undocumented class names couples your code to the implementation and will break on minor releases.

```css
/* INCORRECT */
mds-input-field >>> .message {
  color: red;
}

/* CORRECT */
mds-input-field {
  --mds-input-field-message-color: rgb(var(--magma-danger-text-muted));
  --mds-input-field-message-background: rgb(var(--magma-danger-wash-base));
}
```
