GuideCoreUpdated in v16.7

Forms

Bind ngwr controls to any Angular form and show their errors.

Signal Forms

[formField] binds the control's value (or checked) model, with the schema's state and errors.

import { Component, signal } from '@angular/core';
import { FormField, form, required } from '@angular/forms/signals';
import { WrFormField } from 'ngwr/form';
import { WrOption, WrSelect } from 'ngwr/select';

@Component({
  imports: [FormField, WrFormField, WrSelect, WrOption],
  template: `
    <wr-form-field label="Plan" required>
      <wr-select [formField]="order.plan">
        <wr-option value="free">Free</wr-option>
        <wr-option value="pro">Pro</wr-option>
      </wr-select>
    </wr-form-field>
  `,
})
export class Order {
  private readonly model = signal({ plan: '' });

  protected readonly order = form(this.model, path => {
    required(path.plan, { message: 'Pick a plan.' });
  });
}

Signal Forms rules

Two schema rules from ngwr/validators. Both take { message, when }, like Angular's own.

import { form, required } from '@angular/forms/signals';
import { wrMatches, wrNotBlank } from 'ngwr/validators';

protected readonly account = form(this.model, path => {
  wrNotBlank(path.name);
  required(path.password);
  required(path.confirm);
  wrMatches(path.confirm, path.password, { message: 'The passwords differ.' });
});
RuleReportsError
wrMatches(path, otherPath)A value that differs from the other field's. Silent while its own field is empty.{ kind: 'match', target }, the key match reports.
wrNotBlank(path)An empty or whitespace-only string. Use it in place of required.{ kind: 'required' }, and the field is marked required.

Reactive forms and ngModel

formControlName, [formControl] and [(ngModel)] bind the same model.

<form [formGroup]="form">
  <wr-select formControlName="plan">…</wr-select>
</form>

<wr-select [formControl]="plan">…</wr-select>

<wr-select [(ngModel)]="plan">…</wr-select>

Template validators

required, minlength, maxlength, pattern and email on an ngwr control reach the FormControl, as on an <input>.

<!-- The FormControl gets a required error; the field draws the asterisk. -->
<wr-form-field label="Plan" required>
  <wr-select formControlName="plan" required>…</wr-select>
</wr-form-field>

Angular dropped them before 22.3 (angular/angular#70721), and 22.3 is the lowest version ngwr supports. min and max still never attach: their selectors take input[type=number] only, so put Validators.min / max on the FormControl.

Reactive forms gotchas

Each of these fails silently.

What happensWhat to do
required on <wr-checkbox> accepts an unchecked box.Put Validators.requiredTrue on the FormControl.
Validators.required does not mark the field as required.Add required to <wr-form-field>.
Reactive forms have no read-only state.Bind [readonly] on the control.
<wr-select> matches values with ===, so a copied object shows the placeholder. Bind a primitive key, such as cat.id.
A value outside [min] / [max] stays in the model; the control never corrects it. Use Validators.min / max to flag it.
<wr-slider> ignores null, so reset() leaves the thumb where it was. Reset it to a number.

Validation messages

ngwr ships no error text. <wr-form-field> shows the first of these it finds.

  1. A projected <wr-form-error key="…"> on that field.
  2. The rule's own { message } (Signal Forms).
  3. validation.<key> in your i18n catalog, filled from the error payload. minLength / maxLength read minlength / maxlength.
<wr-form-field label="Handle" required>
  <input wrInput formControlName="handle" />
  <wr-form-error key="required">Pick a handle.</wr-form-error>
</wr-form-field>
// app.config.ts: one message per error key, for every field
provideWrI18nStaticLoader({
  en: {
    validation: {
      required: 'This field is required.',
      minlength: 'Minimum length is {{requiredLength}}.',
      min: 'Enter {{min}} or more.',
    },
  },
}),

With none of them, the error shows no text; dev mode warns.

Focus the first invalid field

After a refused submit, wrFocusFirstInvalid focuses the first field in error and scrolls it into view.

private readonly injector = inject(Injector);
private readonly formEl = viewChild.required<ElementRef<HTMLElement>>('formEl');

protected async save(): Promise<void> {
  const sent = await submit(this.order, async () => this.api.save(this.model()));
  if (!sent) void wrFocusFirstInvalid(this.formEl().nativeElement, { injector: this.injector });
}

Call it after submit() or markAllAsTouched() and pass an injector: it waits for the render that draws the errors. See wrFocusFirstInvalid.

Draw a form from its schema

Describe each field with WR_FIELD next to its rules, and <wr-schema-form> draws it.

import { form, metadata, required } from '@angular/forms/signals';
import { WR_FIELD } from 'ngwr/form';
import { WrSchemaForm } from 'ngwr/schema-form';

protected readonly signup = form(this.model, path => {
  required(path.name, { message: 'Enter your name.' });
  metadata(path.name, WR_FIELD, () => ({ kind: 'input', label: 'Name' }));
  metadata(path.plan, WR_FIELD, () => ({ kind: 'select', label: 'Plan', options: PLANS }));
});
<wr-schema-form [field]="signup" [columns]="2" />
  • A field with no WR_FIELD is not drawn. Write it by hand beside the form.
  • required, disabled, min and max stay schema rules; the spec has no keys for them.
  • A nested object field is skipped. Point a second <wr-schema-form> at it.
  • A section key draws a fieldset, kind: 'array' a repeating group, and hidden() a conditional field.

Your own controls

Writing a control of your own? Check it binds the way ngwr's do.

See also