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),
})
<mat-chip-list> displays a list of values as individual, keyboard accessible, 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.
addOnBlur (optional)Creates a tag from the current input value when focus on the input is lost. Defaults to false .
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With