I've set up my form along with a custom validator that I believe should be functioning correctly. This form consists of 15 fields, out of which the custom validator is designed to refer to three specific ones ('icmp','tcpPorts','udpPorts'). In addition to other mandatory fields, it is necessary for at least ONE of these three fields to be filled in order to proceed with submission.
Below is the code snippet from my component.ts file:
newFWXForm = this.fb.group(
{
sspSelect: ["", Validators.required],
requester: [this.loggedInUser],
requesterContactInfo: [this.loggedInUserEmail],
fwxDescription: ["", Validators.required],
durationTypeSelect: ["Permanent", Validators.required],
durationDate: [""],
infraSelect: [""],
sourceIPs: ["", Validators.required],
DestAnyCheck: [false],
SrcAnyCheck: [false],
icmp: [false],
destinationIPs: ["", Validators.required],
tcpPorts: [],
udpPorts: [],
addDirectory: new FormControl(false),
},
{
Validators: this.atleastOnePortValue("icmp", "tcpPorts", "udpPorts"),
}
);
private atleastOnePortValue( controlNameA: string,controlNameB: string,controlNameC: string): ValidatorFn {
return (control: AbstractControl): ValidationErrors | null => {
const formGroup = control as FormGroup;
const valueOfControlA = formGroup.get(controlNameA)?.value;
const valueOfControlB = formGroup.get(controlNameB)?.value;
const valueOfControlC = formGroup.get(controlNameC)?.value;
if (
valueOfControlA === false &&
valueOfControlB === null &&
valueOfControlC === null
) {
return { atLeastOne: true};
} else {
return null;
}
};
}
Can anyone provide insights or assistance on why this setup is not working as expected? Your help is greatly appreciated, thank you in advance!