[input] Add example of using mat-error with parent formgroup validation
See original GitHub issueBug, feature request, or proposal:
<mat-error>
doesn’t show when I use an email maching validator for emails inputs.
What is the expected behavior? To display the error.
What is the current behavior? No error is displayed
What are the steps to reproduce?
Set a custom validator for check if two emails inputs are equals, like this :
private matchEmail(AC: AbstractControl) {
return AC.get('mail').value === AC.get('mailconfirm').value ? null : { mailmismatch: true };
}
this.administratifForm = this.fb.group({
(...),
mail: this.fb.control('', [Validators.required, Validators.email]),
mailconfirm: this.fb.control('', [Validators.required]),
(...),
}, {
validator: this.matchEmail,
},
);
The template :
<mat-form-field>
<input matInput placeholder="Vérification d'email" formControlName="mailconfirm">
<mat-error *ngIf="administratifForm.get('mailconfirm').hasError('required')">
Ce champ est requis
</mat-error>
<mat-error *ngIf="administratifForm.hasError('mailmismatch')">
Les adresses mail ne correspondent pas
</mat-error>
</mat-form-field>
Which versions of Angular, Material, OS, TypeScript, browsers are affected? Angular 5.0.2, Material 5.0.0-rc0, MacOS Sierra, Firefox
Additional Information
If i replace the <mat-error>
tag by a <p>
tag (or anything else), it’s work.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:9
- Comments:18 (5 by maintainers)
Top Results From Across the Web
Display custom validator error with mat-error - Stack Overflow
The single line of code in this method is quite simple; it returns true (error exists) if the parent control (our FormGroup) is...
Read more >Validating Confirmation Fields in Angular Reactive Forms with ...
The single line of code in this method is quite simple; it returns true (error exists) if the parent control (our FormGroup) is...
Read more >Angular: how to do forms - Medium
The examples will use Angular Material to create an InputFieldComponent. ... Add an errors input and some logic to get the error message:...
Read more >Angular 10, reactive forms with dynamic rows, validation and ...
insert your form group declaration public exampleForm: FormGroup; ... example is used angular material components bud fell free to use it ...
Read more >Angular Custom Form Controls - Complete Guide
formControlName ) as we use for standard input boxes. ... can participate in the parent form validation and value tracking mechanisms.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Ok, I’ve followed the stack post linked by @Jbz797, and here’s the TLDR version of that poorly written post.
.ts
, or import it from somewhere else. Doesn’t matter.[errorStateMatcher]="parentErrorStateMatcher"
to the templateinput
that needs the particular error. Add it to whichevermat-form-field
needs to render that parent error. Eg, forpasswords
group with apassword
control andpassword_verify
control, add it topassword_verify
’s input. Here’s mine.mat-error
that belongs to the GROUP will be rendered. In the example above, it’s the lastmat-error
.Thanks for this thread!
ErrorStateMatcher doesn’t work with custom MatFormFieldControl