1

I have a form and there is a checkbox and a textbox. Textbox is mandatory if checkbox is not checked. So I wrote the below code and it works fine.

autoCodeCheckbox: FormControl = new FormControl();
autoCodeInput: FormControl = new FormControl('', Validators.required);

ngOnInit(): void {
    this.frmExpenseHead = new FormGroup({      
      autoCode: this.autoCodeCheckbox,
      code: this.autoCodeInput      
    });
    
    this.autoCodeCheckbox.valueChanges.subscribe(data => {
      if (data) {
        this.autoCodeInput.setValidators(null);
      }
      else {
        this.autoCodeInput.setValidators(Validators.required);
      }

      this.autoCodeInput.updateValueAndValidity();
    });
}

Now I have another form. There are group of checkboxes and a textbox. Based on textbox value I want the checkbox mandatory. Now I don't know how can I declare checkbox group like above code.

userName: FormControl = new FormControl('');
checkArray: .......

constructor(private fb: FormBuilder) {}
  
ngOnInit(): void {
    this.form = this.fb.group({
      userNameInput: userName,
      checkArray: this.fb.array([], [Validators.required])
    })
}  

2 Answers 2

1

A simple solution creates a reusable component (dumb) that provide the functionality you need. Here u can create a component with a textbox and check box which we can provide a name or any other dynamic data through inputs.

Then u can easily use that component by looping through your main component to render multiple check boxes and text inputs.

Simple example ->

in the main component

constructor(private fb: FormBuilder) {
  this.myFormGroup = this.fb.group({});
}

ngOnInit() {
  //dynamic formcontroller name create by appending index
  [array of those component].map((data, i) => {
    this.myFormGroup.addControl('mySet'+i, this.fb.control(data.propertUNeed))
  })
}

and inside template

<div *ngFor="let ctrl of myFormGroup.controls | keyvalue">
  ..... do what ever thing u need
</div>
Sign up to request clarification or add additional context in comments.

Comments

0

You can also achieve with FormArray and FormGroup.

Your Component.ts file

export class HomeComponent implements OnInit {
  formArray: FormArray;
  formGroups: FormGroup;

  constructor(private fb: FormBuilder) {}

  ngOnInit() {
    this.formGroups = this.fb.group({
      name: '',
      check: this.fb.array([])
    });
    this.addGroup(true, 'John');
    this.addGroup(true, 'David');
    this.addGroup(true, 'Francis');
    this.formGroups.valueChanges.subscribe(() => {
      console.log(this.getGroupArray.controls);
    });
  }

  createGroup(checkText: boolean, name: string = '') {
    return new FormGroup({
      checkbox: new FormControl(checkText),
      name: new FormControl(name)
    });
  }

  addGroup(checkbox: boolean, name: string) {
    this.getGroupArray.push(this.createGroup(checkbox, name));
  }

  get getGroupArray(): FormArray {
    return this.formGroups.get('check') as FormArray;
  }
}

Your component.html file

<form [formGroup]="formGroups">

  <div formArrayName="check">
    <div *ngFor="let check of getGroupArray.controls; let i= index">
      <div [formGroupName]="i">
        <input type="checkbox" formControlName="checkbox">
        <input type="text" formControlName="name">
      </div>
    </div>
  </div>

</form>

Example: https://stackblitz.com/edit/angular-ivy-4gcwgl

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.