diff --git a/apps/forms/61-simplest-signal-form/src/app/app.component.html b/apps/forms/61-simplest-signal-form/src/app/app.component.html
new file mode 100644
index 000000000..459385632
--- /dev/null
+++ b/apps/forms/61-simplest-signal-form/src/app/app.component.html
@@ -0,0 +1,101 @@
+
+
+
Simple Form
+
+
+
+ @if (submittedData()) {
+
+
+ Submitted Data:
+
+
{{ submittedData() | json }}
+
+ }
+
+
diff --git a/apps/forms/61-simplest-signal-form/src/app/app.component.ts b/apps/forms/61-simplest-signal-form/src/app/app.component.ts
index 48edfb268..cc827c4c9 100644
--- a/apps/forms/61-simplest-signal-form/src/app/app.component.ts
+++ b/apps/forms/61-simplest-signal-form/src/app/app.component.ts
@@ -1,160 +1,68 @@
import { JsonPipe } from '@angular/common';
-import { Component, signal, WritableSignal } from '@angular/core';
import {
- FormControl,
- FormGroup,
- ReactiveFormsModule,
- Validators,
-} from '@angular/forms';
+ ChangeDetectionStrategy,
+ Component,
+ signal,
+ WritableSignal,
+} from '@angular/core';
+import {
+ form,
+ FormField,
+ FormRoot,
+ max,
+ min,
+ required,
+} from '@angular/forms/signals';
+
+type UserData = {
+ name: string;
+ lastname: string;
+ age: number | null;
+ note: string;
+};
@Component({
selector: 'app-root',
- imports: [ReactiveFormsModule, JsonPipe],
- template: `
-
-
-
Simple Form
-
-
-
- @if (submittedData()) {
-
-
- Submitted Data:
-
-
{{ submittedData() | json }}
-
- }
-
-
- `,
+ imports: [JsonPipe, FormField, FormRoot],
+ templateUrl: './app.component.html',
+ changeDetection: ChangeDetectionStrategy.OnPush,
})
export class AppComponent {
- form = new FormGroup({
- name: new FormControl('', {
- validators: Validators.required,
- nonNullable: true,
- }),
- lastname: new FormControl('', { nonNullable: true }),
- age: new FormControl(null, [
- Validators.min(1),
- Validators.max(99),
- ]),
- note: new FormControl('', { nonNullable: true }),
- });
+ private readonly _initialData: UserData = {
+ name: '',
+ lastname: '',
+ age: null,
+ note: '',
+ };
+ private _userModel = signal(this._initialData);
- submittedData: WritableSignal<{
- name: string;
- lastname: string;
- age: number | null;
- note: string;
- } | null> = signal(null);
+ protected userForm = form(
+ this._userModel,
+ (schemaPath) => {
+ required(schemaPath.name, { message: 'Name is required' });
+ min(schemaPath.age, 1, { message: 'Age must be at least 1' });
+ max(schemaPath.age, 99, { message: 'Age must be at most 99' });
+ },
+ {
+ submission: {
+ action: async () => {
+ if (this.userForm().valid()) {
+ this.setSubmittedData();
+ }
+ },
+ },
+ },
+ );
+ protected submittedData: WritableSignal = signal(null);
- onSubmit(): void {
- if (this.form.valid) {
- this.submittedData.set(this.form.getRawValue());
- console.log('Form submitted:', this.submittedData);
- }
+ public onReset(): void {
+ this.userForm().reset(this._initialData);
+ this.setSubmittedData();
}
- onReset(): void {
- this.form.reset();
- this.submittedData.set(null);
+ private setSubmittedData(): void {
+ const formData = this._userModel();
+ console.log('Form submitted:', formData);
+ this.submittedData.set(formData);
}
}