# Angular Signal Forms

> Using Eunomia fields in Angular Signal Forms, with formField and a schema that drives their rules and error messages.

Source: https://eunomia-ds.com/guides/angular-signal-forms/

Eunomia fields work in Angular Signal Forms (`@angular/forms/signals`) the same way a native `<input>` does. Bind one with `[formField]`, and the model follows what the user types, picks or checks, while the field shows what the model holds. The schema's rules apply to the field, and its error messages show under it.

This page covers setting it up, which schema rules reach the field, and how errors are shown. For `ngModel`, `formControl` and `formControlName`, see [Angular forms](https://eunomia-ds.com/guides/angular-forms/). For the patterns every field shares regardless of the framework, see [Forms & validation](https://eunomia-ds.com/guides/forms/).

## Setting it up

Import `EunomiaFormField` from `@eunomia/elements/angular/signals` and add it to the `imports` of every component that binds a field, next to Angular's own `FormField`:

```ts
import { CUSTOM_ELEMENTS_SCHEMA, Component, signal } from "@angular/core";
import { FormField, form, required, email } from "@angular/forms/signals";
import { EunomiaFormField } from "@eunomia/elements/angular/signals";
import "@eunomia/elements/input.js";
import "@eunomia/elements/switch.js";

@Component({
  selector: "app-profile",
  imports: [FormField, EunomiaFormField],
  schemas: [CUSTOM_ELEMENTS_SCHEMA],
  templateUrl: "./profile.component.html",
})
export class ProfileComponent {
  readonly model = signal({ email: "", newsletter: false });

  readonly profile = form(this.model, (path) => {
    required(path.email, { message: "Enter your email" });
    email(path.email, { message: "Enter a valid email address" });
  });
}
```

```html
<eun-input label="Email" type="email" [formField]="profile.email"></eun-input>
<eun-switch label="Newsletter" [formField]="profile.newsletter"></eun-switch>
```

It requires Angular 21 or later, the first version with Signal Forms. Without it, Angular finds nothing to connect the field to and throws that the element `is an invalid [formField] directive host`.

`CUSTOM_ELEMENTS_SCHEMA` is still needed, as for any Eunomia tag, and so is importing each component you use (see [Get started](https://eunomia-ds.com/get-started/)). Importing `@eunomia/elements/angular/signals` registers no component on its own, so it's safe to import during a server render.

> **Two entry points**
>
> `@eunomia/elements/angular` is for `ngModel`, `formControl` and `formControlName`, from Angular 18. `@eunomia/elements/angular/signals` is for `[formField]`. Import the one your form uses, or both in an app that mixes them.

## What each field holds

The model holds the same values as a reactive control would: a `string` for `eun-input`, a `Date` or `null` for `eun-date-picker`, a `number` for `eun-counter`, a `boolean` for `eun-checkbox`, `eun-check-card` and `eun-switch`, and so on. The full table is in What each field holds on [Angular forms](https://eunomia-ds.com/guides/angular-forms/).

A group of radios binds through its `eun-radio-list`, rather than through each `eun-radio`:

```html
<eun-radio-list name="plan" label="Plan" [formField]="profile.plan">
  <eun-radio value="free">Free</eun-radio>
  <eun-radio value="pro">Pro</eun-radio>
</eun-radio-list>
```

Setting the model from code (`model.set()`, `profile.email().value.set()`) updates the field without counting as a change from the user, so the field stays pristine.

## The schema drives the field

The schema's rules apply to the field, so you declare each one once, in the schema, rather than on the tag:

| Schema rule | On the field |
| --- | --- |
| `required()` | `required`, with its asterisk |
| `disabled()` | `disabled` |
| `readonly()` | `readonly` |
| `min()`, `max()` | `min`, `max` on `eun-input`, `eun-counter` and `eun-slider` |
| `max()` | `max` on `eun-rating` |
| `minLength()`, `maxLength()` | `minlength`, `maxlength` on `eun-input` and `eun-textarea` |

A rule applied only `when` a condition holds follows it: the field becomes required, then optional again, as the condition changes. While no rule sets one of these properties, the field keeps its own, so a static `required` on the tag still holds.

`hidden()` doesn't apply: remove a hidden field from the template with `@if`, as Angular recommends.

## Errors

### When errors appear

A field shows its error once the user has left it, as described under When errors appear in [Forms & validation](https://eunomia-ds.com/guides/forms/). The form drives that moment too:

| What happens | Result |
| --- | --- |
| The user leaves the field | The field becomes touched, and shows its error |
| `markAsTouched()` | The field shows its error |
| `submit()` | Every field shows its error, even one the user never reached |
| `reset()` | The field hides its error again |

A checkbox, a check card, a switch, a checkbox list and a radio list show their error as soon as they change, before they're left, as they do outside a form.

Submit through `submit()`, which marks every field touched before it runs your action, and only runs it while the form is valid:

```ts
import { submit } from "@angular/forms/signals";

save(event: Event): void {
  event.preventDefault();
  submit(this.profile, async () => {
    await this.api.save(this.model());
  });
}
```

```html
<form (submit)="save($event)">
  …
  <eun-button type="submit">Save</eun-button>
</form>
```

### Which message is shown

The field shows the `message` of every schema error on it, in the order of the schema:

```ts
validate(path.username, ({ value }) =>
  value() === "admin"
    ? { kind: "reserved", message: "This username is reserved" }
    : null,
);
```

An error without a `message`, such as `required(path.email)` alone, shows the field's own message for that rule, in the library's locale, when the field checks it too: `required`, `min`, `max`, `minLength` and `maxLength` on the fields listed above. Any other error without a `message` shows the error styling alone, with no text. Give your schema's errors a `message` to be sure of what the user reads.

There's no need to bind `[error]` yourself: the field shows the schema's messages as they change, and stops showing them once the rule passes.

### The schema is the only source of validation

`profile().valid()` accounts for the schema's rules only. A rule the field checks by itself, from an attribute or a property the schema doesn't know about, is shown under the field but isn't reported to the form:

| On the field | Declare in the schema |
| --- | --- |
| `type="email"` | `email()` |
| `pattern` | `pattern()` |
| `required`, `min`, `max`, ... | `required()`, `min()`, `max()`, ... |
| The `validators` property | `validate()`, or `validateStandardSchema()` |

> **Declare every rule in the schema**
>
> A field with `type="email"` but no `email()` rule shows its own error on an invalid address, while the form stays valid and `submit()` runs its action. Declare the rule in the schema, which then also sets its message.

## Disabling a field

Disable a field through the schema with `disabled()`, rather than binding `[disabled]` on the tag: the form sets the field's `disabled` itself, and would override the binding.

```ts
readonly profile = form(this.model, (path) => {
  disabled(path.city, ({ valueOf }) => !valueOf(path.country));
});
```

`readonly()` works the same way.

## Resetting a form

`profile().reset()` marks every field untouched and pristine, so they hide their errors again. It leaves the model as it is, unless you pass it the value to start over from:

```ts
reset(): void {
  this.profile().reset({ email: "", newsletter: false });
}
```

Each bound field is `controlled`, so a native reset of the form leaves its value to the model rather than restoring the field's own `default-value`. Give the model its initial value, not the field.

## See also

- [Angular forms](https://eunomia-ds.com/guides/angular-forms/): binding fields with `ngModel`, `formControl` and `formControlName`
- [Forms & validation](https://eunomia-ds.com/guides/forms/): labels, native constraints and validators, and when errors appear
- [Get started](https://eunomia-ds.com/get-started/): installing the library and using a component in Angular
- [Signal Forms](https://angular.dev/guide/forms/signals/overview): Angular's own guide to Signal Forms
