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])
})
}