Scenario:
At the start, there is a single text box named Name1
, a date picker called DOB1
, and a check box labeled Compare
. Both Name1
and DOB1
are mandatory. When the checkbox is clicked, two new form controls are dynamically included, named Name2
and DOB2
, and at least one of either Name1
or DOB2
becomes required.
Hence, a valid form must have any of the following combinations:
Name1
DOB1
Name2
or //IfName2
is valid then need to remove required validator fromDOB2
Name1
DOB1
DOB2
or //IfDOB2
is valid then need to remove required validator fromName2
Name1
DOB1
Name2
DOB2
In all these cases, when the form is valid, the submit button should be enabled.
Issue:
I attempted to use setValidators but still cannot determine what I am missing. Upon clicking the checkbox, the form is only considered valid if all four controls are valid, whereas I require just three of them to be valid.
Code:
<form [formGroup]="profileForm" (ngSubmit)="onSubmit()">
<ion-card class="person1">
<ion-card-content>
<ion-list lines="full" class="ion-no-margin ion-no-padding">
<ion-item>
<ion-label position="stacked">Name / Number <ion-text color="danger">*</ion-text>
</ion-label>
<ion-input type="text" formControlName="NameNumber"></ion-input>
</ion-item>
<ion-item>
<ion-label position="stacked">Date of birth<ion-text color="danger">*</ion-text>
</ion-label>
<ion-datetime required placeholder="Select Date" formControlName="DateOfBirth"></ion-datetime>
</ion-item>
</ion-list>
</ion-card-content>
</ion-card>
<ion-card class="person2" *ngIf="isComparisonChecked">
<ion-card-content>
<ion-list lines="full" class="ion-no-margin ion-no-padding">
<ion-item>
<ion-label position="stacked">Name / Number <ion-text color="danger">*</ion-text>
</ion-label>
<ion-input type="text" formControlName="NameNumber2"></ion-input>
</ion-item>
<ion-item>
<ion-label position="stacked">Date of birth<ion-text color="danger">*</ion-text>
</ion-label>
<ion-datetime required placeholder="Select Date" formControlName="DateOfBirth2"></ion-datetime>
</ion-item>
</ion-list>
</ion-card-content>
</ion-card>
<ion-item class="compare-section" lines="none">
<ion-label>Compare</ion-label>
<ion-checkbox color="danger" formControlName="IsCompare"></ion-checkbox>
</ion-item>
<div class="ion-padding">
<ion-button color="danger" *ngIf="LicensedStatus" [disabled]="!this.profileForm.valid" expand="block"
type="submit" class="ion-no-margin">Submit</ion-button>
</div>
</form>
Ts:
profileForm = new FormGroup({
NameNumber: new FormControl('', [Validators.required, Validators.pattern('^[A-Za-z0-9 _]*[A-Za-z0-9][A-Za-z0-9 _]*$')]),
DateOfBirth: new FormControl('', Validators.required),
IsCompare: new FormControl(false)
});
...
this.profileForm.get('IsCompare').valueChanges.subscribe(checked => {
if (checked) {
this.profileForm.addControl('NameNumber2', new FormControl('', [Validators.required, Validators.pattern('^[A-Za-z0-9 _]*[A-Za-z0-9][A-Za-z0-9 _]*$')]));
this.profileForm.addControl('DateOfBirth2', new FormControl('', Validators.required));
this.profileForm.get('NameNumber2').valueChanges.subscribe(() => {
if (this.profileForm.get('NameNumber2').valid) {
this.profileForm.get('DateOfBirth2').clearValidators();
}
else {
this.profileForm.get('DateOfBirth2').setValidators([Validators.required]);
}
this.profileForm.get('DateOfBirth2').updateValueAndValidity();
});
this.profileForm.get('DateOfBirth2').valueChanges.subscribe(() => {
if (this.profileForm.get('DateOfBirth2').valid) {
this.profileForm.get('NameNumber2').clearValidators();
}
else {
this.profileForm.get('NameNumber2').setValidators([Validators.required, Validators.pattern('^[A-Za-z0-9 _]*[A-Za-z0-9][A-Za-z0-9 _]*$')]);
}
this.profileForm.get('NameNumber2').updateValueAndValidity();
});
}
else {
this.profileForm.removeControl('NameNumber2');
this.profileForm.removeControl('DateOfBirth2');
}
});
What could be the missing piece here?
Update #1:
I made amendments in the above code. If I utilize updateValueAndValidity
, an error message is encountered in the console.