Im trying validate the form field value based on the other field so i have written a custom validator when i tried to get the value other field it throws error i have tried the following code kindly help me thanks in advance
export class CreatesessionComponent implements OnInit {
eventform : FormGroup ;
constructor(private formBuilder: FormBuilder) {
}
ngOnInit() {
this.eventform = this.formBuilder.group({
eventname : new FormControl(''[Validators.required,Validators.pattern('[A-Za-z]+')]),
userlive : new FormControl('',[Validators.required,this.maxuser]),
totaluser :new FormControl('',[Validators.required,this.totaluser])
});
}
totaluser(control : FormGroup) : {[s:string ]: boolean} {
console.log(control.controls['eventname'].value)
// if(control.value > 20){
// return { total : true };
// }
return null;
}
gives error in the console as cannot read the property of 'eventname' undefined
I am currently doing something like this also. In order to get it to work I get the group for the control and then search that group for the required control
totaluser(control : AbstractControl) : {[s:string ]: boolean} {
const group = control.parent;
if (group) {
console.log(group.controls['eventname'].value)
}
return null;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With