Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular 2 validation based on other field value?

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

like image 608
Arun Kumaresh Avatar asked Mar 10 '23 09:03

Arun Kumaresh


1 Answers

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;
}
like image 77
Fairlie Agile Avatar answered Mar 20 '23 01:03

Fairlie Agile