I have my form and my custom validator right here, with logic that I think should work. I have 15 fields, 3 in which the custom validator refers to ('icmp','tcpPorts','udpPorts'). Along with the other required fields I need at least ONE of those three fields filled to submit.
here is the code in my component.ts
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;
}
};
}
Any knowledge or help on why this still isn't working? Very much appreciated and thank you in advanced!