working with angular materials mat-form-field
and reactive forms. In a project I have a recurring pattern that looks like this
// ts
this.formGroup = this.formBuilder.group({
name: ['', ServerValidation]
})
<!-- html -->
<div [formGroup]="formGroup">
<mat-form-field>
<input
matInput
formControlName="name"
[placeholder]="'Name'"
name="name"
/>
<mat-error
*ngIf="
formGroup
.get('name')
.hasError('serverValidation')
"
>
{{
formGroup
.get("name")
.getError("serverValidation")
}}
</mat-error>
</mat-form-field>
</div>
This is a high level - accepting that I can receive validation errors from the server - how can i repeat this http template pattern in a component? I have a hunch that I should utilise ControlValueAccessor - but do not know how to do so.
The implementation I imagine might look something like this
<!-- html -->
<div [formGroup]="formGroup">
<serverValidatedInput formControlName="'name'">
<mat-error>error message for client side validation</mat-error>
</serverValidatedInput>
</div>
So essentially I want to use this custom component like a regular material input (more-or-less), except that it comes with the server validation error by default. Could anyone give me some direction here - thanks. :)