1

I have a list, simple - string[]. I generate checkbox for every item of that list. Then I create FormControls:

this.list.map((item) => {
  this.form.addControl(item, new FormControl(false, Validators.required))
})  

But I want to add a Validator to control the allowed number of checked checkboxes, so I assume I could do this if I move those FormControls into the FormGroup, so I try something like:

  constructor(private fb:FormBuilder) { 
        this.form = this.fb.group({
          input1Ctrl: ['', Validators.required],
          input2Ctrl: ['',Validators.required],
          checkboxGroup : new FormGroup({})   
        })         
        this.list.map((item) => {
          this.form.checkboxGroup.addControl(item, new FormControl(false, Validators.required))
        })  

But that gives:

Supplied parameters do not match any signature of call target. Property 'checkboxGroup' does not exist on type 'FormGroup'.

How should I do it?

5
  • Why this.form.checkboxGroup? That is perfectly invalid. And that is why you have the error. Commented Sep 4, 2017 at 12:35
  • this.form.get('checkboxGroup').addControl(...) Commented Sep 4, 2017 at 13:18
  • Thanks @developer033, But now I get - Property 'addControl' does not exist on type 'AbstractControl'... Thats strange, because I provided the FormGroup type for checkboxGroup... Commented Sep 4, 2017 at 13:40
  • @user8558489 (this.form.get('checkboxGroup') as FormGroup).addControl(...) Commented Sep 4, 2017 at 13:41
  • @user8558489 - Perfect (just had to store (this.form.get('checkboxGroup') as FormGroup in local variable and then addControl. Feel free to post an answer :) Commented Sep 4, 2017 at 13:46

2 Answers 2

2

Access checkboxGroup like this.form.controls['checkboxGroup'].

  constructor(private fb:FormBuilder) { 
            this.form = this.fb.group({
              input1Ctrl: ['', Validators.required],
              input2Ctrl: ['',Validators.required],
              checkboxGroup : new FormGroup({})   
            })         
            this.list.map((item) => {
              this.form.controls['checkboxGroup'].addControl(item, new FormControl(false, Validators.required))
            })

EDIT : Created plunker for the above

https://plnkr.co/edit/nxo52y?p=preview

Sign up to request clarification or add additional context in comments.

3 Comments

The same as when using the dot
@user8558489 I have created a plunker. You can check the console. No issues with it. It just works fine.
Youre using FormGroup here not formBuilder's fb.formGroup so yes it is different. See angular.io/api/forms/FormBuilder#control
0

You're using FormGroup here not formBuilder's group method.

It seems you would set it up like:

this.form = this.fb.group({
   checkboxGroup: this.fb.group({})
})

Accessing FormBuilder's group and FormGroup both work for me like: this.form.get('checkboxGroup') and `this.form.controls['checkboxGroup']

See https://angular.io/api/forms/FormBuilder#control

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.