Skip to content

Commit 119dd4b

Browse files
kamarouskikamarouski
and
kamarouski
authored
feat(material/form-field): Ability to support custom error message components inside a form field (#25399)
* feat(material/form-field): Ability to support custom error message components inside a form field Adds an ability to use custom error components tagged with [matError] attributes inside a form field. This is useful when a reusable custom error message template is needed for form fields. Example: <mat-form-field> <mat-label>Label</mat-label> <input matInput> <custom-error matError>Error message</custom-error> </mat-form-field> * feat(material/form-field): Ability to support custom error message components inside a form field Adds an ability to use custom error components tagged with [matError] attributes inside a form field. This is useful when a reusable custom error message template is needed for form fields. Example: <mat-form-field> <mat-label>Label</mat-label> <input matInput> <custom-error matError>Error message</custom-error> </mat-form-field> * feat(material/form-field): Ability to support custom error message components inside a form field Adds an ability to use custom error components tagged with [matError] attributes inside a form field. This is useful when a reusable custom error message template is needed for form fields. Example: <mat-form-field> <mat-label>Label</mat-label> <input matInput> <custom-error matError>Error message</custom-error> </mat-form-field> Co-authored-by: kamarouski <kamarouski@google.com>
1 parent d496ebe commit 119dd4b

File tree

4 files changed

+8
-6
lines changed

4 files changed

+8
-6
lines changed

src/material/form-field/directives/error.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export const MAT_ERROR = new InjectionToken<MatError>('MatError');
1919

2020
/** Single error message to be shown underneath the form-field. */
2121
@Directive({
22-
selector: 'mat-error',
22+
selector: 'mat-error, [matError]',
2323
host: {
2424
'class': 'mat-mdc-form-field-error mat-mdc-form-field-bottom-align',
2525
'aria-atomic': 'true',

src/material/form-field/form-field.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@
8383
[ngSwitch]="_getDisplayedMessages()">
8484
<div class="mat-mdc-form-field-error-wrapper" *ngSwitchCase="'error'"
8585
[@transitionMessages]="_subscriptAnimationState">
86-
<ng-content select="mat-error"></ng-content>
86+
<ng-content select="mat-error, [matError]"></ng-content>
8787
</div>
8888

8989
<div class="mat-mdc-form-field-hint-wrapper" *ngSwitchCase="'hint'"

src/material/form-field/testing/shared.spec.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,9 @@ export function runHarnessTests(
189189

190190
fixture.componentInstance.requiredControl.setValue('');
191191
dispatchFakeEvent(fixture.nativeElement.querySelector('#with-errors input'), 'blur');
192-
expect(await formFields[1].getTextErrors()).toEqual(['Error 1', 'Error 2']);
192+
expect(await formFields[1].getTextErrors()).toEqual(
193+
isMdcImplementation ? ['Error 1', 'Error 2'] : ['Error 1'],
194+
);
193195
});
194196

195197
it('should be able to get hint messages of form-field', async () => {
@@ -271,7 +273,7 @@ export function runHarnessTests(
271273
<input matInput [formControl]="requiredControl">
272274
273275
<mat-error>Error 1</mat-error>
274-
<mat-error>Error 2</mat-error>
276+
<div matError>Error 2</div>
275277
<mat-hint align="start">Hint 1</mat-hint>
276278
<mat-hint align="end">Hint 2</mat-hint>
277279
</mat-form-field>

tools/public_api_guard/material/form-field.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export class MatError {
5959
// (undocumented)
6060
id: string;
6161
// (undocumented)
62-
static ɵdir: i0.ɵɵDirectiveDeclaration<MatError, "mat-error", never, { "id": "id"; }, {}, never, never, false>;
62+
static ɵdir: i0.ɵɵDirectiveDeclaration<MatError, "mat-error, [matError]", never, { "id": "id"; }, {}, never, never, false>;
6363
// (undocumented)
6464
static ɵfac: i0.ɵɵFactoryDeclaration<MatError, [{ attribute: "aria-live"; }, null]>;
6565
}
@@ -147,7 +147,7 @@ export class MatFormField implements AfterContentInit, AfterContentChecked, Afte
147147
// (undocumented)
148148
_textPrefixContainer: ElementRef<HTMLElement>;
149149
// (undocumented)
150-
static ɵcmp: i0.ɵɵComponentDeclaration<MatFormField, "mat-form-field", ["matFormField"], { "hideRequiredMarker": "hideRequiredMarker"; "color": "color"; "floatLabel": "floatLabel"; "appearance": "appearance"; "subscriptSizing": "subscriptSizing"; "hintLabel": "hintLabel"; }, {}, ["_labelChildNonStatic", "_labelChildStatic", "_formFieldControl", "_prefixChildren", "_suffixChildren", "_errorChildren", "_hintChildren"], ["mat-label", "[matPrefix], [matIconPrefix]", "[matTextPrefix]", "*", "[matTextSuffix]", "[matSuffix], [matIconSuffix]", "mat-error", "mat-hint:not([align='end'])", "mat-hint[align='end']"], false>;
150+
static ɵcmp: i0.ɵɵComponentDeclaration<MatFormField, "mat-form-field", ["matFormField"], { "hideRequiredMarker": "hideRequiredMarker"; "color": "color"; "floatLabel": "floatLabel"; "appearance": "appearance"; "subscriptSizing": "subscriptSizing"; "hintLabel": "hintLabel"; }, {}, ["_labelChildNonStatic", "_labelChildStatic", "_formFieldControl", "_prefixChildren", "_suffixChildren", "_errorChildren", "_hintChildren"], ["mat-label", "[matPrefix], [matIconPrefix]", "[matTextPrefix]", "*", "[matTextSuffix]", "[matSuffix], [matIconSuffix]", "mat-error, [matError]", "mat-hint:not([align='end'])", "mat-hint[align='end']"], false>;
151151
// (undocumented)
152152
static ɵfac: i0.ɵɵFactoryDeclaration<MatFormField, [null, null, null, null, null, { optional: true; }, { optional: true; }, null]>;
153153
}

0 commit comments

Comments
 (0)