Angular forms
Eunomia fields work in Angular forms the same way a native <input>
does. Bind one with formControlName, formControl or ngModel, and
the form follows what the user types, picks or checks, while the field
shows what the form holds. Disabling, validating and resetting the
form apply to the field as well.
This page covers setting it up, what each field puts in the form, and
how validation and error messages fit together. For the patterns every
field shares regardless of the framework, see
[formField], see
Setting it up
Import EunomiaValueAccessor from @eunomia/elements/angular and add
it to the imports of every standalone component (or NgModule) that
binds a field, next to ReactiveFormsModule or FormsModule:
import { CUSTOM_ELEMENTS_SCHEMA, Component } from "@angular/core";
import { ReactiveFormsModule } from "@angular/forms";
import { EunomiaValueAccessor } from "@eunomia/elements/angular";
import "@eunomia/elements/input.js";
@Component({
selector: "app-profile",
imports: [ReactiveFormsModule, EunomiaValueAccessor],
schemas: [CUSTOM_ELEMENTS_SCHEMA],
templateUrl: "./profile.component.html",
})
export class ProfileComponent {}
It requires Angular 18 or later. Without it, Angular finds nothing to
connect the control to and throws
No value accessor for form control.
CUSTOM_ELEMENTS_SCHEMA is still needed, as for any Eunomia tag, and
so is importing each component you use (see
@eunomia/elements/angular registers no component on its own, so it's
safe to import during a server render.
ngDefaultControl is the usual workaround for a custom element, but it
only listens for the native input event and writes value, which
misses most fields and every checkbox. Once EunomiaValueAccessor is
imported, Angular uses it in its place, so you can remove
ngDefaultControl.
Reactive forms
Bind each field with formControlName inside a [formGroup], or with
[formControl] on its own:
import { FormControl, FormGroup, Validators } from "@angular/forms";
export class ProfileComponent {
readonly form = new FormGroup({
email: new FormControl("", Validators.required),
country: new FormControl("fr"),
plan: new FormControl("pro"),
channels: new FormControl<string[]>(["email"]),
newsletter: new FormControl(false),
});
save(): void {
if (this.form.valid) {
console.log(this.form.value);
}
}
}
<form [formGroup]="form" (ngSubmit)="save()">
<eun-input formControlName="email" label="Email" type="email"></eun-input>
<eun-select formControlName="country" label="Country">
<option value="fr">France</option>
<option value="be">Belgium</option>
</eun-select>
<eun-radio-list formControlName="plan" name="plan" label="Plan">
<eun-radio value="free">Free</eun-radio>
<eun-radio value="pro">Pro</eun-radio>
</eun-radio-list>
<eun-checkbox-list formControlName="channels" label="Contact me by">
<eun-checkbox value="email" label="Email"></eun-checkbox>
<eun-checkbox value="sms" label="SMS"></eun-checkbox>
</eun-checkbox-list>
<eun-switch formControlName="newsletter" label="Newsletter"></eun-switch>
<eun-button type="submit">Save</eun-button>
</form>
Setting a value from code (setValue, patchValue, reset) updates
the field without counting as a change from the user, so the control
stays pristine.
Template-driven forms
[(ngModel)] works the same way. Inside a <form>, give each field a
name, as Angular requires for any ngModel:
<form #profile="ngForm" (ngSubmit)="save(profile.value)">
<eun-input name="name" label="Name" required [(ngModel)]="name"></eun-input>
<eun-checkbox
name="terms"
label="I accept the terms"
required
[(ngModel)]="terms"
></eun-checkbox>
<eun-button type="submit">Save</eun-button>
</form>
What each field holds
The control's value has the same type as the field's own value, except
for the fields you check, whose value is their checked state:
| Field | Value in the control |
|---|---|
eun-input, eun-textarea, eun-input-phone, eun-color-picker |
string |
eun-select |
string, or string[] with multiple |
eun-date-picker, eun-time-picker |
Date or null |
eun-counter, eun-slider, eun-rating |
number |
eun-input-file |
An array of File, URL string or already stored file |
eun-checkbox-list |
string[] of the checked values |
eun-radio-list |
The value of the checked radio |
eun-checkbox, eun-check-card, eun-switch |
boolean |
A group of radios binds through its eun-radio-list, which is what
groups them, rather than through each eun-radio. A control set to
null, as a reset does, empties its field and unchecks every child of
a list.
Validation
The field's own rules count
Angular's validators work as usual. The field's own rules count too:
required, type="email", pattern, min, max and the
validators property. While one of them fails, the control holds an
eunomia error, so form.valid accounts for it with nothing to
repeat in Angular:
this.form.controls.email.errors;
// { eunomia: { message: "Enter a valid email address", reasons: ["typeMismatch"] } }
message is the text the field shows, in the library's locale, and
reasons lists every ValidityState flag currently set
(valueMissing, typeMismatch, patternMismatch, tooShort,
rangeUnderflow, customError, ...).
With required on the field, Angular's own required validator also
applies, so a required field left empty holds both required and
eunomia. On a checkbox or a switch, only eunomia reports it, since
Angular treats false as a value.
After changing a field's validators from code, call updateValueAndValidity() on its control so Angular checks it again.
Showing Angular's messages
A field shows the messages of its own rules by itself. For a rule that
lives only in Angular, a custom validator or an error returned by your
API, pass its message to the field through error:
<eun-input
formControlName="username"
label="Username"
[error]="
form.controls.username.hasError('taken')
? 'This username is already taken'
: undefined
"
></eun-input>
A message passed through error isn't reported back as an eunomia
error, so the control becomes valid again as soon as the rule passes.
When errors appear
A field shows its error once the user has left it or tried to submit,
as described under When errors appear in
| What happens | Result |
|---|---|
| The user leaves the field | The control becomes touched, and the field shows its error |
markAsTouched() or markAllAsTouched() |
The field shows its error |
| The form is submitted | Every field shows its error, even one the user never reached |
markAsUntouched() or a Reset button |
The field hides its error again |
markAsDirty() or markAsPristine() |
The field's dirty follows the control |
Resetting from code hides the errors too, with one exception before
Angular 21: form.reset() keeps an error that a submit revealed on a
field the user never left, the same way Angular keeps its own
submitted state. Call resetForm() on the FormGroupDirective
instead (on an NgForm, from Angular 20) to clear both, or use a
Reset button, which clears them in every version.
Angular adds novalidate to every form, so an invalid form still
submits: check form.valid in your ngSubmit handler, as in the
reactive example above.
Disabling a field
Disable a reactive control from code with disable() and enable(),
or create it disabled with new FormControl({ value: "", disabled: true }),
rather than binding [disabled], which Angular warns against on a
reactive field. With ngModel, bind [disabled]:
<eun-select name="city" label="City" [disabled]="!country" [(ngModel)]="city">
…
</eun-select>
Resetting a form
form.reset(), resetForm() and a type="reset" button reset the
control, and the field follows. Each bound field is controlled, so a
native reset of the form leaves its value to Angular rather than
restoring the field's own default-value. Give the control its initial
value, not the field.
See also
-
Angular Signal Forms : binding fields with[formField]and a schema -
Forms & validation : labels, native constraints and validators, and when errors appear -
Get started : installing the library and using a component in Angular -
Angular forms : Angular's own guide to reactive and template-driven forms