I have the following form with custom validators:
this.Form = this.fb.group({
cruiseId: new FormControl(this.leg.cruiseId),
startDate: new FormControl(this.leg.startDate, [Validators.required]),
endDate: new FormControl(this.leg.endDate)
}, { validators: [this.checkDuplicates, this.checkDates] });
In my component, I have an input property which contains all departure dates for a cruise (@Input() cruiseArray!: cruiseItem[];
). Using the checkDuplicates function, I want to verify that we don't have 2 identical departure dates for the same cruise.
checkDuplicates(group: FormGroup) {
console.log(this.cruiseArray);
let sDate = group.get('startDate')?.value;
if (sDate !== null && this.cruiseArray.find(x => x.startDate === sDate))
{
return { invalidDuplicate: true }
}
return null;
}
My concern is that this.cruiseArray
is alway undefined.
If I try the following in my component
ngOnInit(): void {
console.log(this.cruiseArray);
}
it works perfectly and my array returned by the parent is populated.
Full code:
@Component({
selector: ..,
templateUrl: ..,
styleUrls: [..]
})
export class MyComponent implements OnInit {
Input() cruiseArray!: cruiseItem[];
....
ngOnInit(): void {
console.log(this.cruiseArray); <--- DOES WORK
}
....
ngOnChanges(changes: SimpleChanges) {
this.createForm();
}
....
createForm() {
this.Form = this.fb.group({
cruiseId: new FormControl(this.leg.cruiseId),
startDate: new FormControl(this.leg.startDate, [Validators.required]),
endDate: new FormControl(this.leg.endDate)
}, { validators: [this.checkDuplicates, this.checkDates] });
}
....
checkDuplicates(group: FormGroup) {
console.log(this.cruiseArray); <--- DOES NOT WORK
let sDate = group.get('startDate')?.value;
if (sDate !== null && this.cruiseArray.find(x => x.startDate === sDate))
{
return { invalidDuplicate: true }
}
return null;
}
}
}
Why this.cruiseArray
is undefined in my validator function even when it is populated elsewhere in my component.