I have one formGroup that have an array:
createForm() {
this.form = this.formBuilder.group({
name: [[], [Validators.required]]
})
}
I have a list of checkbox that when it is checked, i add in this array the value, but when i do console.log(form.controls('name').valid) it returns false but i have elements in this formControl:
<mat-list-item *ngFor="let name of myNames">
<mat-checkbox (click)="addItemInArray(name.id)">{{ name.name }}</mat-checkbox>
</mat-list-item>
addItemInArray(id: number) {
const indexElem = this.form.get('name').value.indexOf(id);
if (indexElem === -1) {
this.form.get('name').value.push(id)
} else {
this.form.get('name').value.splice(indexElem, 1)
}
I'm looking if there's a way to make this control valid when i have at least one element inside the array.