I am new to angular and I am currently building a website in angular, Im facing a problem that I cant solve with the angular documentation Im not quite understanding what's going on behind the problem.
In few words, I have a formArray(updateAddress) which contains a formGroup and some formControl's inside and I'm having a hard time trying to make the validations for it.
Code HTML:
<form [formGroup]="profileForm">
<div *ngFor="let add of updateAddress().controls; let addIndex=index">
<div formArrayName="updateAddress">
<div [formGroupName]="addIndex">
<label>
Rua:
<input type="text" formControlName="address">
</label>
<div
*ngIf="profileForm.get('add.address')?.invalid && (address.get('add.address')?.dirty || profileForm.get('add.address')?.touched)">
<div *ngIf="profileForm.get('add.address')?.errors?.required">
Tem de escrever a rua da sua loja.
</div>
</div>
<button (click)="removeAddress(addIndex)">Remover</button>
</div>
</div>
</div>
<button type="button" (click)="addAddress()">Adicionar Morada</button>
</form>
Code TS:
profileForm: FormGroup;
control: any;
constructor(
public formBuilder: FormBuilder,
private cd: ChangeDetectorRef
) {
this.profileForm = this.formBuilder.group({
file: [''],
name: new FormControl('', [Validators.required, Validators.pattern('[a-zA-Z ]*')]),
email: new FormControl('', [Validators.required, Validators.pattern("^[a-z0-9._%+-]+@[a-z0-9.-]+\\.[a-z]{2,4}$")]),
password: new FormControl('',[Validators.required, Validators.minLength(7),Validators.pattern('(?=.*[A-Za-z])(?=.*[0-9])(?=.*[$@$!#^~%*?&,.<>"\'\\;:\{\\\}\\\[\\\]\\\|\\\+\\\-\\\=\\\_\\\)\\\(\\\)\\\`\\\/\\\\\\]])[A-Za-z0-9\d$@].{7,}')]),
password2: new FormControl('', Validators.required),
nif: new FormControl('',[Validators.required, Validators.pattern('\\d{9}')]),
birthDate: new FormControl('',[Validators.required, Validators.pattern("^(0[1-9]|[12][0-9]|3[01])[/.](0[1-9]|1[012])[/.](19|20)\\d\\d$") ]),
phoneNumber: new FormControl('', [Validators.required, Validators.pattern('\\d{9}')]),
updateAddress: new FormArray([], Validators.required)
});
}
updateAddress(): FormArray {
return this.profileForm.get("updateAddress") as FormArray
}
newAddress(): FormGroup {
return this.formBuilder.group({
address: new FormControl('', Validators.required),
address2: new FormControl('', Validators.required),
postalCode: new FormControl('', Validators.required),
city: new FormControl('', Validators.required),
area: new FormControl('', Validators.required)
})
}
addAddress() {
this.updateAddress().push(this.newAddress());
}
removeAddress(addIndex:number) {
this.updateAddress().removeAt(addIndex);
}
I really cant solve the problem in this validation because the validation feels like it goes through but doesnt appear any error in the webpage also I dont know if Im approaching this the right way or not aswell.
If someone can guide me or help I'll gladly appreciate it!
UPDATE