0

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
  • 2,148
  • 4
  • 24
  • 49
  • 1
    I don't see the method `setParent` getting called anywhere in the code that you gave. Can you post that part as well? – Scrambo Aug 17 '16 at 14:45
  • TBH I dont have that. What exactly is Setparent? I am searching for it, but documentation seems to be a bit slim on it. Any good resources? Edit: I see its a method I apply to a class(?). Should I apply it to the class that initializes the form? Thanks so much! – Andrew Graham-Yooll Aug 17 '16 at 14:55
  • Edit2: [And I see this](https://angular.io/docs/dart/latest/api/angular2.web_worker.worker/AbstractControl/setParent.html) which doesnt really help me – Andrew Graham-Yooll Aug 17 '16 at 15:04
  • My apologies, seems like that was some internal call. (Also, the link you gave me is referencing the Dart version of Angular, not TS). After looking through your code again, you have the `kidsData` defined as solely a type of `any` object but then you're treating it as an array? [Looking at this](https://www.typescriptlang.org/docs/handbook/basic-types.html) it seems like you need to define it as `any[]` instead? – Scrambo Aug 17 '16 at 18:43
  • and after going through the Dynamic Forms link, the issue might be that the `params` parameter might need an array on it as well? as in `params: any[]` because right now, you're calling `.forEach()` on something that may or may not be an array. – Scrambo Aug 17 '16 at 18:49

2 Answers2

1

To go along with my comment, after doing some reading on Typescript Types, and looking at the Forms example you gave, I might have figured out what the problem is.

You have a .forEach() operating on params: any, a variable that may or may not be an array. Try slapping on the [] like so: params: any[], and see if that helps.

Scrambo
  • 579
  • 5
  • 17
  • Thanks for the help @Scrambo, but it didn't solve the issue. I ended up moving the if/else statements in a for loop instead of the ForEach method. For the moment it works, but I am aware it is not pretty. – Andrew Graham-Yooll Aug 19 '16 at 13:49
0

Maybe you just need to specify rith this (second parameter in forEach function)?

The invocation context (this) of the forEach function call

Community
  • 1
  • 1
Vadim Levkovsky
  • 336
  • 2
  • 14