Skip to content

Commit 0ba09ec

Browse files
msmallestcrisbeto
authored andcommitted
docs: add signal form example + copy for matInput (#33779)
(cherry picked from commit cbc5e96)
1 parent 916000e commit 0ba09ec

6 files changed

Lines changed: 58 additions & 2 deletions

File tree

src/components-examples/material/input/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export {InputClearableExample} from './input-clearable/input-clearable-example';
22
export {InputErrorStateMatcherExample} from './input-error-state-matcher/input-error-state-matcher-example';
33
export {InputErrorsExample} from './input-errors/input-errors-example';
4+
export {InputErrorsSignalFormExample} from './input-errors-signal-form/input-errors-signal-form-example';
45
export {InputFormExample} from './input-form/input-form-example';
56
export {InputHintExample} from './input-hint/input-hint-example';
67
export {InputOverviewExample} from './input-overview/input-overview-example';
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
.example-form {
2+
min-width: 150px;
3+
max-width: 500px;
4+
width: 100%;
5+
}
6+
7+
.example-full-width {
8+
width: 100%;
9+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<form class="example-form">
2+
<mat-form-field class="example-full-width">
3+
<mat-label>Email</mat-label>
4+
<input type="email" matInput [formField]="emailForm" placeholder="Ex. pat@example.com">
5+
@if (emailForm().getError('email') && !emailForm().getError('required')) {
6+
<mat-error>Please enter a valid email address</mat-error>
7+
}
8+
@if (emailForm().getError('required')) {
9+
<mat-error>Email is <strong>required</strong></mat-error>
10+
}
11+
</mat-form-field>
12+
</form>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import {Component, signal} from '@angular/core';
2+
import {MatInputModule} from '@angular/material/input';
3+
import {MatFormFieldModule} from '@angular/material/form-field';
4+
import {email, form, FormField, required} from '@angular/forms/signals';
5+
6+
/**
7+
* @title Input with error messages (signal form)
8+
*/
9+
@Component({
10+
selector: 'input-errors-signal-form-example',
11+
templateUrl: 'input-errors-signal-form-example.html',
12+
styleUrl: 'input-errors-signal-form-example.css',
13+
imports: [MatFormFieldModule, MatInputModule, FormField],
14+
})
15+
export class InputErrorsSignalFormExample {
16+
private _emailModel = signal('');
17+
18+
protected emailForm = form(this._emailModel, schemaPath => {
19+
(required(schemaPath), email(schemaPath));
20+
});
21+
}

src/components-examples/material/input/input-errors/input-errors-example.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {MatInputModule} from '@angular/material/input';
44
import {MatFormFieldModule} from '@angular/material/form-field';
55

66
/**
7-
* @title Input with error messages
7+
* @title Input with error messages (reactive form)
88
*/
99
@Component({
1010
selector: 'input-errors-example',

src/material/input/input.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ be used with `matNativeControl`:
3030
* url
3131
* week
3232

33+
### Use with Angular Forms
34+
35+
`matInput` is compatible with `@angular/forms` and supports `FormField`, `FormsModule`, and `ReactiveFormsModule`.
36+
3337
### Form field features
3438

3539
There are a number of `<mat-form-field>` features that can be used with any `<input matNativeControl>` or
@@ -53,11 +57,20 @@ with your `matNativeControl`. By default, these error messages are shown when th
5357
the user has interacted with (touched) the element or the parent form has been submitted. If
5458
you wish to override this behavior (e.g. to show the error as soon as the invalid control is dirty
5559
or when a parent form group is invalid), you can use the `errorStateMatcher` property of the
56-
`matNativeControl`. The property takes an instance of an `ErrorStateMatcher` object. An `ErrorStateMatcher`
60+
`matNativeControl`. The property takes an instance of an `ErrorStateMatcher` object.
61+
62+
For reactive forms, an `ErrorStateMatcher`
5763
must implement a single method `isErrorState` which takes the `FormControl` for this `matNativeControl` as
5864
well as the parent form and returns a boolean indicating whether errors should be shown. (`true`
5965
indicating that they should be shown, and `false` indicating that they should not.)
6066

67+
For signal forms, an `ErrorStateMatcher`
68+
must still implement the method `isErrorState` for backwards compatability with the reactive forms API,
69+
but it can be fullfilled with `isErrorState() {return false}`.
70+
Then the custom matcher can implement `isSignalErrorState`, which takes the `Field` for this
71+
`matNativeControl` as well as the parent form and returns a boolean indicating whether errors
72+
should be shown. (`true` indicating that they should be shown, and `false` indicating that they should not.)
73+
6174
<!-- example(input-error-state-matcher) -->
6275

6376
A global error state matcher can be specified by setting the `ErrorStateMatcher` provider. This

0 commit comments

Comments
 (0)