# KptFormField (kpt-form-field)
A form field wrapper: the label, the hint and the error message around any control
(`kpt-input`, for instance). Presentational - the error state is passed in through `error`.
Import: `import { KptFormField } from '@konce-pt/angular';`
## Selector
`kpt-form-field`
## Inputs (input signals)
- `label`: string - the label text
- `hint`: string - a hint (hidden while there is an error)
- `error`: string | null - the error message; when non-empty, the field is marked invalid
- `required`: boolean - an asterisk next to the label
- `controlId`: string - optional; the control's id for `label[for]` (library controls report theirs)
- `labelType`: 'fixed' | 'over' | 'in' | 'on' - the label variant (default `fixed`)
- `enterMotion`: 'spring' | 'fade' | 'none' - how the message and the error appear (default from `provideKptMotion`, else `spring`)
## Label variants (labelType)
- `fixed` (the default) - a static label above the control (as before).
- `over` - a float label: the label rises above the field's border.
- `in` - a float label: the label rises into the upper part inside the field (a taller field, the "filled" style).
- `on` - a float label: the label sits on the top border (a notch, the "outlined" style).
The float variants animate the label's transition (from its resting position where the placeholder is ->
raised). It rises when the control takes focus **or** when the field has a value. Detecting whether
it is filled happens inside the component (`input`/`change` events + the initial value) - it does **not**
require setting a placeholder on the control.
### Controls with no native `input`/`textarea`
The value of `kpt-select` cannot be read from the DOM (the panel lives in an overlay), so such a control
reports being filled itself - it provides the `KPT_FORM_FIELD_CONTROL` token with a `filled` signal.
`kpt-select` does this out of the box; a custom control needs:
import { KPT_FORM_FIELD_CONTROL, type KptFormFieldControl } from '@konce-pt/angular';
@Component({
providers: [{ provide: KPT_FORM_FIELD_CONTROL, useExisting: forwardRef(() => MyControl) }],
})
export class MyControl implements KptFormFieldControl {
readonly filled = computed(() => this.value() != null);
}
Once a control provides the token, `kpt-form-field` stops looking into the DOM and trusts the
`filled` signal alone - which is why the label rises the instant the value changes.
## Linking the control (label, hint, error)
`kpt-form-field` provides the `KPT_FORM_FIELD` context to its content, and every library control
uses it: the control reports its `id` (its own, or a generated one) and `label[for]` points at it,
so **`controlId` is optional**. A control on a `div` with a role (`kpt-select`, `kpt-datepicker`,
`kpt-radio-group`, `kpt-rich-text`...) is not named by `label[for]`, so it gets `aria-labelledby`
pointing at the label. The visible hint or error has an `id` and lands in the control's
`aria-describedby` - a screen reader hears it when it reaches the field, not only when the error appears.
A custom control links itself with `kptFieldA11y`:
import { kptFieldA11y } from '@konce-pt/angular';
readonly id = input(null);
protected readonly field = kptFieldA11y(this.id, 'my-control');
// template: [id]="field.id()" [attr.aria-labelledby]="field.labelledBy()"
// [attr.aria-describedby]="field.describedBy()"
`controlId` given explicitly still wins, and a control without its own `id` takes it.
## Content projection
The control goes into the content (ng-content):
## Integration with Signal Forms
`error` usually comes from a form field, for example:
emailError = computed(() => {
const s = loginForm.email();
return s.touched() && s.invalid() ? s.errors()[0]?.message ?? null : null;
});
The `touched()` gate applies to the message only - do not gate the validators (`when:` on
`state.touched()` makes `valid()` lie on an untouched form). The `kpt-*` controls gate their own
visual state the same way. The full submit flow: the
`## A form from scratch (Signal Forms)` section in the root llms.txt.
## Motion
`enterMotion` (`spring | fade | none`):
- the message under the field enters on every change - hint -> error, error A -> error B - sliding in
from above and sharpening out of a blur (`fade`: only fades); each new error is a new element, so it
is also announced again;
- `spring` only: the control shakes briefly when the field turns invalid - not when the error text
changes and not for an error present on load; the floating label rises on a spring.
Messages present on the first render do not animate. With no value it takes `enter` from `provideKptMotion`
(default `spring`); `enabled: false` and the system "reduce motion" setting always win. Every form
control's surface (border, focus ring, background) changes smoothly on hover, focus and error - a shared
rule, independent of this input.
## Tokens
`--kpt-form-field-label`, `--kpt-form-field-hint`, `--kpt-form-field-error`, `--kpt-color-danger`.
The float variants add: `--kpt-form-field-placeholder` (the resting label),
`--kpt-form-field-border-focus` (raised), `--kpt-form-field-border-error` (the error state),
`--kpt-form-field-bg` (the notch mask in `on`). Animation: `--kpt-motion-duration-fast`,
`--kpt-motion-easing-standard`; it respects `prefers-reduced-motion`.
## Accessibility
- The error message carries `role="alert"`.
- The label, hint and error are linked to the control automatically (`label[for]` or `aria-labelledby`,
plus `aria-describedby`) - see "Linking the control" above.