ComponentUpdated in v15

Form

A plain layout wrapper for a label, a control and its error.

Installation

Import the class, and @use the entry point in your global styles: without it the component renders unstyled.

import { Component } from '@angular/core';
import { WrFormError, WrFormItem } from 'ngwr/form';

@Component({ imports: [WrFormError, WrFormItem] })
export class MyComponent {}

Which wrapper — item or field?

WrapperUse it when
<wr-form-field>By default. It reads the control, shows its errors and wires the label.
<wr-form-item>You want the layout only. You set hasError and write the error yourself.

Basic usage

<wr-form-item>
  <label>Email</label>
  <input wrInput type="email" [(ngModel)]="email" />
</wr-form-item>

With error

[hasError] turns the label and input red; project the message as <wr-form-error>.

Validate
<wr-form-item [hasError]="invalid()">
  <label>Email</label>
  <input wrInput type="email" [(ngModel)]="email" />
  <wr-form-error>Please enter a valid email.</wr-form-error>
</wr-form-item>

Form Item API

NameDescriptionTypeDefault
hasErrorColour the label and input as an error.booleanfalse

Form Error API

NameDescriptionTypeDefault
keyValidator key this message corresponds to (e.g. 'required'). Optional — without a key the message always renders, which is the plain inline-error usage inside <wr-form-item>.string—

<wr-form-field> API — for reference

Also in ngwr/form; the examples are on its own page.

NameDescriptionTypeDefault
labelLabel text shown above the control.string''
hintHint text shown below the control. Hidden when an error is visible.string''
requiredShow a * next to the label.booleanfalse
optionalShow an "optional" marker next to the label — the word comes from optionalLabel. Mutually exclusive with required.booleanfalse
optionalLabelWord inside the optional marker's parentheses. Falls back to form.optional, then 'optional'. The parentheses are the template's, so no locale has to repeat punctuation.string | nullnull
controlIdForce a specific id on the label's for attribute. Auto-generated otherwise, and adopted by the projected wrInput unless that element already carries an id of its own.string`wr-form-field-${++uid}`
autoErrorsRender a message for any error the markup does not already answer: the validator's own message, else validation.<key> from the app's i18n catalog. ngwr ships no error texts, so a key neither of them answers renders nothing, and dev mode warns once for it. On by default: a field with no <wr-form-error> at all is the common case. Turn it off for a field whose copy is entirely hand-written.booleantrue

CSS variables

Set these on the component's selector or a wrapper, not on :root: the defaults are declared on ngwr/form's own element.

VariableDefaultDeclared on
--wr-form-error-font-sizevar(--wr-text-xs).wr-form-error
--wr-form-error-line-heightvar(--wr-text-xs-leading).wr-form-error
--wr-form-field-error-font-sizevar(--wr-text-sm).wr-form-field
--wr-form-field-error-line-heightvar(--wr-leading-tight).wr-form-field
--wr-form-field-errors-gapvar(--wr-space-xs).wr-form-field
--wr-form-field-font-familyvar(--wr-font-family-base).wr-form-field
--wr-form-field-gapvar(--wr-space-sm).wr-form-field
--wr-form-field-hint-font-sizevar(--wr-text-xs).wr-form-field
--wr-form-field-hint-line-heightvar(--wr-leading-tight).wr-form-field
--wr-form-field-label-font-sizevar(--wr-text-sm).wr-form-field
--wr-form-field-label-font-weightvar(--wr-font-weight-medium).wr-form-field
--wr-form-field-label-gapvar(--wr-space-xs).wr-form-field
--wr-form-field-label-line-heightvar(--wr-text-sm-leading).wr-form-field
--wr-form-field-optional-font-sizevar(--wr-text-sm).wr-form-field
--wr-form-field-optional-font-weightvar(--wr-font-weight-regular).wr-form-field
--wr-form-item-gapvar(--wr-space-sm).wr-form-item
--wr-form-item-label-font-sizevar(--wr-text-sm).wr-form-item
--wr-form-item-label-font-weightvar(--wr-font-weight-medium).wr-form-item
--wr-form-item-label-line-heightvar(--wr-text-sm-leading).wr-form-item

Dependencies

See also