diff --git a/apps/forms/64-form-array/src/app/app.component.ts b/apps/forms/64-form-array/src/app/app.component.ts index f6133d3df..b99a23746 100644 --- a/apps/forms/64-form-array/src/app/app.component.ts +++ b/apps/forms/64-form-array/src/app/app.component.ts @@ -6,68 +6,43 @@ import { WritableSignal, } from '@angular/core'; import { - AbstractControl, - FormArray, - FormControl, - FormGroup, - ReactiveFormsModule, - Validators, -} from '@angular/forms'; -import { ContactFormComponent } from './contact-form.component'; - -type ContactFormGroup = FormGroup<{ - firstname: FormControl; - lastname: FormControl; - relation: FormControl; - email: FormControl; -}>; - -type EmailFormGroup = FormGroup<{ - type: FormControl; - email: FormControl; -}>; + applyEach, + email, + form, + FormField, + FormRoot, + required, + SchemaPathTree, + validate, +} from '@angular/forms/signals'; -type RegistrationForm = { - name: FormControl; - pseudo: FormControl; - contacts: FormArray; - emails: FormArray; -}; - -type RegistrationValue = { - name: string; - pseudo: string; - contacts: Array<{ - firstname: string; - lastname: string; - relation: string; - email: string; - }>; - emails: Array<{ - type: string; - email: string; - }>; -}; +import { ContactFormComponent } from './contact-form.component'; +import { Contact, Email, Registration } from './types'; -export const minLengthArray = (min: number) => { - return (c: AbstractControl) => { - if (c.value.length >= min) return null; +function ContactSchema(item: SchemaPathTree) { + required(item.firstName, { message: 'This field is required' }); + required(item.lastname, { message: 'This field is required' }); + required(item.relation, { message: 'This field is required' }); + required(item.email, { message: 'Email is required' }); + email(item.email, { message: 'Enter a valid email' }); +} - return { MinLengthArray: true }; - }; -}; +function EmailSchema(item: SchemaPathTree) { + required(item.type, { message: 'This field is required' }); + required(item.email, { message: 'Email is required' }); + email(item.email, { message: 'Enter a valid email' }); +} @Component({ selector: 'app-root', - imports: [ReactiveFormsModule, JsonPipe, ContactFormComponent], + imports: [JsonPipe, ContactFormComponent, FormField, FormRoot], changeDetection: ChangeDetectionStrategy.OnPush, template: `

Registration form

Profile

@@ -78,12 +53,14 @@ export const minLengthArray = (min: number) => { + [formField]="registrationForm.name" /> - @if (showError(form.controls.name)) { - This field is required + @if ( + registrationForm.name().invalid() && + (registrationForm.name().touched() || + registrationForm.name().dirty()) + ) { + {{ registrationForm.name().errors()[0].message }} } @@ -93,12 +70,14 @@ export const minLengthArray = (min: number) => { + [formField]="registrationForm.pseudo" /> - @if (showError(form.controls.pseudo)) { - This field is required + @if ( + registrationForm.pseudo().invalid() && + (registrationForm.pseudo().touched() || + registrationForm.pseudo().dirty()) + ) { + {{ registrationForm.pseudo().errors()[0]?.message }} } @@ -116,8 +95,8 @@ export const minLengthArray = (min: number) => {
-
- @for (contact of contacts.controls; track $index) { +
+ @for (contact of registrationForm.contacts; track contact) { { }
- @if (contacts.invalid && (contacts.touched || contacts.dirty)) { -

At least one contact is required.

+ @if ( + registrationForm.contacts().invalid() && + (registrationForm.contacts().touched() || + registrationForm.contacts().dirty()) + ) { +

+ {{ registrationForm.contacts().errors()[0]?.message }} +

} @@ -138,8 +123,8 @@ export const minLengthArray = (min: number) => {
-
- @for (email of emails.controls; track $index) { +
+ @for (emailField of registrationForm.emails; track emailField) {
@@ -156,39 +141,44 @@ export const minLengthArray = (min: number) => {
-
+
+