diff --git a/apps/forms/65-signal-form-edition/src/app/user-form.component.ts b/apps/forms/65-signal-form-edition/src/app/user-form.component.ts index e9a6f91cb..e1fd4f835 100644 --- a/apps/forms/65-signal-form-edition/src/app/user-form.component.ts +++ b/apps/forms/65-signal-form-edition/src/app/user-form.component.ts @@ -1,24 +1,20 @@ import { ChangeDetectionStrategy, Component, - effect, inject, input, + linkedSignal, } from '@angular/core'; import { rxResource } from '@angular/core/rxjs-interop'; -import { - FormControl, - FormGroup, - ReactiveFormsModule, - Validators, -} from '@angular/forms'; +import { form, FormField, min, required, submit } from '@angular/forms/signals'; import { Router } from '@angular/router'; -import { of } from 'rxjs'; +import { lastValueFrom, of } from 'rxjs'; import { FakeBackendService } from './fake-backend.service'; +import { User } from './user.model'; @Component({ selector: 'app-user-form', - imports: [ReactiveFormsModule], + imports: [FormField], template: `

@@ -30,7 +26,7 @@ import { FakeBackendService } from './fake-backend.service'; class="h-8 w-8 animate-spin rounded-full border-4 border-indigo-500 border-t-transparent">

} @else { -
+
+
+
+
+
+
@@ -124,50 +133,51 @@ export class UserFormComponent { defaultValue: undefined, }); - userForm = new FormGroup({ - firstname: new FormControl('', { - nonNullable: true, - validators: [Validators.required], - }), - lastname: new FormControl('', { - nonNullable: true, - validators: [Validators.required], - }), - age: new FormControl(0, { - nonNullable: true, - validators: [Validators.required, Validators.min(0)], - }), - grade: new FormControl(0, { - nonNullable: true, - validators: [Validators.required], - }), + userModel = linkedSignal>(() => { + const hasValue = this.userResource.hasValue(); + const user = this.userResource.value(); + + if (hasValue && user) { + // eslint-disable-next-line @typescript-eslint/no-unused-vars + const { id, ...rest } = user; + return rest; + } else { + return { + firstname: '', + lastname: '', + age: 0, + grade: 0, + }; + } + }); + + userSignalForm = form(this.userModel, (schemePath) => { + required(schemePath.firstname, { message: 'Firstname is required' }); + required(schemePath.lastname, { message: 'Lastname is required' }); + required(schemePath.age); + min(schemePath.age, 0, { message: 'Age must be positive' }); + required(schemePath.grade); }); - constructor() { - effect(() => { + onSubmit(event: Event): void { + event.preventDefault(); + + submit(this.userSignalForm, async () => { const userValue = this.userResource.value(); + if (userValue) { - this.userForm.patchValue(userValue); + await lastValueFrom( + this.backend.updateUser({ + ...this.userModel(), + id: userValue.id, + }), + ); } else { - this.userForm.reset({ firstname: '', lastname: '', age: 0, grade: 0 }); + await lastValueFrom(this.backend.addUser(this.userModel())); } - }); - } - onSubmit(): void { - if (this.userForm.valid) { - const userValue = this.userResource.value(); - const obs = userValue - ? this.backend.updateUser({ - ...this.userForm.getRawValue(), - id: userValue.id, - }) - : this.backend.addUser(this.userForm.getRawValue()); - - obs.subscribe(() => { - this.router.navigate(['/']); - }); - } + this.router.navigate(['/']); + }); } onCancel(): void {