Function within Array#forEach

12 views
Skip to first unread message

Andrew Graham-Yooll

unread,
Aug 17, 2016, 11:13:54 AM8/17/16
to AngularJS

I am generating a form in Angular2/Ionic2 and I have followed the dynamic form tutorial on the Angular site. However, for my needs, I need more validations instead of just 'required'. Hence this code.

doGenerateKidsBasicFormWithNameAndAge(params: any){
let kidsData:any = {};

params.forEach(question => {

        kidsData[question.id] = this.doDecideValidation(question)
        });
  return new FormGroup(kidsData);
}

This is the function that decideds what type of validation will be applied.

doDecideValidation(question){
    if(question.type === "text"){
      new FormControl(question || '', Validators.compose([Validators.required, Validators.minLength(question.minLength), Validators.maxLength(question.maxLength), Validators.pattern('[a-zA-Z ]*')]));
    }else if(question.type === "number"){
      new FormControl(question || '', Validators.compose([Validators.required]));
    }else {
      new FormControl(question || '');
    }
  };

When I do this, I get an error.

TypeError: Cannot read property 'setParent' of undefined

Any ideas?

Andrew Graham-Yooll

unread,
Aug 17, 2016, 11:16:31 AM8/17/16
to AngularJS
Addition: I am aware of a setParent(){} method. But Angular's documentation is pretty slim on it. Docs Here
Reply all
Reply to author
Forward
0 new messages