I'm migrating to Angular 7 from angular 5 and i found that using ngmodel and formcontrolName in the same element is deprecated in Angular6 and removed in 7. Now I cannot set validators to my mat-chip input from angular material
html:
<div class="form-group col-md-6">
<label class="required"> User Names
</label>
<mat-form-field >
<mat-chip-list class="form-control form-control-sm"
[ngClass]="{'is-invalid':form.controls.names.invalid && (form.controls.names.touched || form.controls.names.dirty) }"
#chipList3>
<mat-chip *ngFor="let local of form.get('names').value" [removable]="removable"
(remove)="remove_names(local)">
{{local}}
<mat-icon matChipRemove *ngIf="removable">cancel</mat-icon>
</mat-chip>
<input [matChipInputFor]="chipList3"
[matChipInputSeparatorKeyCodes]="separatorKeysCodes" [matChipInputAddOnBlur]="addOnBlur"
(matChipInputTokenEnd)="add_names($event)" />
</mat-chip-list>
</mat-form-field>
</div>
before migrating to angular 7 I would just use formControlName in the input tag like this
<div class="form-group col-md-6">
<label class="required"> User Names
</label>
<mat-form-field >
<mat-chip-list class="form-control form-control-sm"
[ngClass]="{'is-invalid':form.controls.names.invalid && (form.controls.names.touched || form.controls.names.dirty) }"
#chipList3>
<mat-chip *ngFor="let local of user.names" [removable]="removable"
(remove)="remove_names(local)">
{{local}}
<mat-icon matChipRemove *ngIf="removable">cancel</mat-icon>
</mat-chip>
<input [matChipInputFor]="chipList3"
formControlName="names"
[matChipInputSeparatorKeyCodes]="separatorKeysCodes" [matChipInputAddOnBlur]="addOnBlur"
(matChipInputTokenEnd)="add_names($event)" />
</mat-chip-list>
</mat-form-field>
</div>
I do all my custom validations when the user pushes the name into the list but i want to chekc whether it is present or not for that i use Validators.required but now since i use the formcontrol value itself to display the list i cannot give any refernce to formControlName in the template
TS:
user :FormGroup =this.fb.group({
names:[[], [Validators.required]],
id:0
});
Now even if there are values in the formControl it doesent satisfy Validators.required
After spending time in research I found that adding this
this.form.controls['names'].updateValueAndValidity()
satisfied Validators.required but now i get this error
Error: No value accessor for form control with name: 'names'