Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular Material mat-chip validation

How do I validate if the user has selected a chip in the mat-chip-list and also only when the user selects a chip form should become valid. Right now I have the validators.required set on the control, but it doesn't work. This is what I have done so far:


HTML

<mat-chip-list formControlName="packageName">
   <mat-chip *ngFor="let pkg of packages" selected="true" style="background-color:#8d6e63">
        {{pkg.value}}
   </mat-chip>
</mat-chip-list>

TS

ngOnInit() {
  this.form = new FormGroup({
    'packageName': new FormControl('', Validators.required),
})
like image 547
Sumchans Avatar asked Apr 21 '18 20:04

Sumchans


People also ask

What is Mat chip list?

<mat-chip-list> displays a list of values as individual, keyboard accessible, chips.

What is angular material contact chips?

The md-contact-chips, an Angular Directive, is an input control built on md-chips and uses the md-autocomplete element. The contact chip component accepts a query expression which returns a list of possible contacts. The user can select one of these and add it to the list of availble chips.

What is addOnBlur?

addOnBlur (optional)Creates a tag from the current input value when focus on the input is lost. Defaults to false .


1 Answers

You could use form.status to get the form status VALID or INVALID or form.controls.packageName.errors to get an array of the formControl errors or null if no errors.

Here is a running example using your code.

like image 124
ibenjelloun Avatar answered Oct 05 '22 06:10

ibenjelloun