I had formArray
checkbox on my checkboxForm
, I need to disabled button submit if no checkbox are checked
, I had implement custom validator
When you are initializing the form in the constructor add the validator to the formarray first.Then after you receive the data call the addCheckboxes
method.
form: FormGroup;
receivedSummons = [];
constructor(private formBuilder: FormBuilder) {
this.checkboxForm = this.formBuilder.group({
receivedSummons: new FormArray([], minSelectedCheckboxes(1))
});
of(this.someMethodWhichReturnReceivedSummons()).subscribe(receivedSummons => {
this.receivedSummons = receivedSummons;
this.addCheckboxes();
});
}
In the addCheckboxes
private addCheckboxes() {
this.receivedSummons.map((o, i) => {
const control = new FormControl(i === 0); // if first item set to true, else false
(this.form.controls.receivedSummons as FormArray).push(control);
});
}