Skip to content

Commit 3d62432

Browse files
committed
feat: use declarative code to submit the form
1 parent 218a335 commit 3d62432

2 files changed

Lines changed: 29 additions & 19 deletions

File tree

apps/forms/61-simplest-signal-form/src/app/app.component.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<div class="mx-auto max-w-md rounded-lg bg-white p-8 shadow-md">
33
<h1 class="mb-6 text-3xl font-bold text-gray-900">Simple Form</h1>
44

5-
<form class="space-y-6" (ngSubmit)="onSubmit($event)" novalidate>
5+
<form class="space-y-6" [formRoot]="userForm">
66
<div>
77
<label for="name" class="mb-2 block text-sm font-medium text-gray-700">
88
Name
@@ -73,7 +73,6 @@ <h1 class="mb-6 text-3xl font-bold text-gray-900">Simple Form</h1>
7373
<div class="flex gap-4">
7474
<button
7575
type="submit"
76-
(click)="onSubmit($event)"
7776
[disabled]="userForm().invalid()"
7877
class="flex-1 rounded-md bg-blue-600 px-4 py-2 font-medium text-white transition hover:bg-blue-700 disabled:cursor-not-allowed disabled:bg-gray-400">
7978
Submit

apps/forms/61-simplest-signal-form/src/app/app.component.ts

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,45 +5,56 @@ import {
55
signal,
66
WritableSignal,
77
} from '@angular/core';
8-
import { form, FormField, max, min, required } from '@angular/forms/signals';
8+
import {
9+
form,
10+
FormField,
11+
FormRoot,
12+
max,
13+
min,
14+
required,
15+
} from '@angular/forms/signals';
916

1017
type UserData = {
1118
name: string;
1219
lastname: string;
13-
age: number;
20+
age: number | null;
1421
note: string;
1522
};
1623

1724
@Component({
1825
selector: 'app-root',
19-
imports: [JsonPipe, FormField],
26+
imports: [JsonPipe, FormField, FormRoot],
2027
templateUrl: './app.component.html',
2128
changeDetection: ChangeDetectionStrategy.OnPush,
2229
})
2330
export class AppComponent {
2431
private readonly _initialData: UserData = {
2532
name: '',
2633
lastname: '',
27-
age: NaN,
34+
age: null,
2835
note: '',
2936
};
3037
private _userModel = signal<UserData>(this._initialData);
3138

32-
protected userForm = form(this._userModel, (schemaPath) => {
33-
required(schemaPath.name, { message: 'Name is required' });
34-
min(schemaPath.age, 1, { message: 'Age must be at least 1' });
35-
max(schemaPath.age, 99, { message: 'Age must be at most 99' });
36-
});
39+
protected userForm = form(
40+
this._userModel,
41+
(schemaPath) => {
42+
required(schemaPath.name, { message: 'Name is required' });
43+
min(schemaPath.age, 1, { message: 'Age must be at least 1' });
44+
max(schemaPath.age, 99, { message: 'Age must be at most 99' });
45+
},
46+
{
47+
submission: {
48+
action: async () => {
49+
if (this.userForm().valid()) {
50+
this.setSubmittedData();
51+
}
52+
},
53+
},
54+
},
55+
);
3756
protected submittedData: WritableSignal<UserData | null> = signal(null);
3857

39-
public onSubmit(event: Event): void {
40-
event.preventDefault();
41-
42-
if (this.userForm().valid()) {
43-
this.setSubmittedData();
44-
}
45-
}
46-
4758
public onReset(): void {
4859
this.userForm().reset(this._initialData);
4960
this.setSubmittedData();

0 commit comments

Comments
 (0)