Consider an Angular Form inside a component with a fully managed errors and controls. This form is used to add and edit* certain values in a particular scenario.
<form #addConfigurationFormRef="ngForm"
(ngSubmit)="onFormSubmit(addConfigurationFormRef)">
<mat-form-field>
<input matInput
ngModel
[(ngModel)]="configuration.signatory"
name="signatory"
placeholder="Certificate Signatory"
autocomplete="off"
required
maxlength="100"
minlength="3" />
</mat-form-field>
<mat-card-actions
<button mat-raised-button
color="accent"
[disabled]="addConfigurationFormRef.invalid || pageSubmit">
{{ formActionButtonLabel }}
</button>
</mat-card-actions>
</form>
Note that edit operation is undertaken the data has already present in the database.
public formActionButtonLabel: string = "Add Configurations";
public isEditComponent: Boolean = false;
public onFormSubmit(form: NgForm): void{ //logic };
public configuration: CfConfiguration = {};
ngOnInit() {
this.fetchCommonDataService.data$.subscribe(data =>{
if(data) this.isEditComponent = true;
if(data) this.configuration = data;
});
}
Problem: During edit operation the form is prefilled by data binding. Now consider that some of the fields are now invalid, they do not get the red highlight since the form is still untouched.
Requirement: I want that as soon as the existing data gets placed in the form, the invalid field should be highlighted.
Using Angular 7 and Material CDK