I'm currently encountering a peculiar problem with using mat-chip-list with inputs. My form group consists of two form controls: contacts and name.
this.form = this.formBuilder.group({
name: ['', [Validators.required]],
contactIds: ['', [Validators.required]]
})
The layout of this form is as follows:
<mat-form-field #contactsChipList>
<mat-chip-list>
<mat-chip *ngFor="let contact of contacts" [removable]="removable" (remove)="removeChipListItem(contact)">
{{ contact.name | titlecase }} {{ contact.surname | titlecase }}
<mat-icon matChipRemove *ngIf="removable"></mat-icon>
</mat-chip>
<input [matChipInputFor]="contactsChipList" placeholder="Contacts" formControlName="contactIds" />
<mat-error *ngIf="form.hasError('required', 'contactIds')">This field is required</mat-error>
</mat-chip-list>
</mat-form-field>
Issue: When I clear all the elements from the input field, the parent form (formGroup) is flagged as invalid, but the error property of the formGroup remains empty. As a result, the error message is not displayed.
Alternative Attempt: However, when I utilize a standard input element with a matInput attribute instead of a mat-chip-list, the error is properly shown upon clearing the field.
Here is the markup for this scenario:
<div class="form-group">
<mat-form-field>
<input matInput placeholder="Contacts" formControlName="contactIds" />
<mat-error *ngIf="form.hasError('required', 'contactIds')">This field is required</mat-error>
</mat-form-field>
</div>
Assumption: I suspect the issue is related to the mat-chip-list element. I have attempted to investigate the following:
@Input()errorStateMatcher: ErrorStateMatcher
but I am unsure how to implement it. Unfortunately, my online searches have not been very helpful.
Has anyone else faced a similar problem? Feel free to ask for further clarification.