Skip to content

Commit 511f076

Browse files
docs(form-field): Use Form Validation in custom control example (#18844)
1 parent f9fffab commit 511f076

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

src/components-examples/material-experimental/mdc-form-field/mdc-form-field-custom-control/form-field-custom-control-example.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {FocusMonitor} from '@angular/cdk/a11y';
22
import {coerceBooleanProperty} from '@angular/cdk/coercion';
33
import {Component, ElementRef, Input, OnDestroy, Optional, Self} from '@angular/core';
4-
import {FormBuilder, FormGroup, ControlValueAccessor, NgControl} from '@angular/forms';
4+
import {FormBuilder, FormGroup, ControlValueAccessor, NgControl, Validators} from '@angular/forms';
55
import {MatFormFieldControl} from '@angular/material-experimental/mdc-form-field';
66
import {Subject} from 'rxjs';
77

@@ -78,8 +78,8 @@ export class MyTelInput implements ControlValueAccessor, MatFormFieldControl<MyT
7878

7979
@Input()
8080
get value(): MyTel | null {
81-
const {value: {area, exchange, subscriber}} = this.parts;
82-
if (area.length === 3 && exchange.length === 3 && subscriber.length === 4) {
81+
if (this.parts.valid) {
82+
const {value: {area, exchange, subscriber}} = this.parts;
8383
return new MyTel(area, exchange, subscriber);
8484
}
8585
return null;
@@ -97,9 +97,9 @@ export class MyTelInput implements ControlValueAccessor, MatFormFieldControl<MyT
9797
@Optional() @Self() public ngControl: NgControl) {
9898

9999
this.parts = formBuilder.group({
100-
area: '',
101-
exchange: '',
102-
subscriber: '',
100+
area: [null, [Validators.required, Validators.minLength(3), Validators.maxLength(3)]],
101+
exchange: [null, [Validators.required, Validators.minLength(3), Validators.maxLength(3)]],
102+
subscriber: [null, [Validators.required, Validators.minLength(4), Validators.maxLength(4)]],
103103
});
104104

105105
_focusMonitor.monitor(_elementRef, true).subscribe(origin => {

src/components-examples/material/form-field/form-field-custom-control/form-field-custom-control-example.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {FocusMonitor} from '@angular/cdk/a11y';
22
import {coerceBooleanProperty} from '@angular/cdk/coercion';
33
import {Component, ElementRef, Input, OnDestroy, Optional, Self} from '@angular/core';
4-
import {FormBuilder, FormGroup, ControlValueAccessor, NgControl} from '@angular/forms';
4+
import {FormBuilder, FormGroup, ControlValueAccessor, NgControl, Validators} from '@angular/forms';
55
import {MatFormFieldControl} from '@angular/material/form-field';
66
import {Subject} from 'rxjs';
77

@@ -78,8 +78,8 @@ export class MyTelInput implements ControlValueAccessor, MatFormFieldControl<MyT
7878

7979
@Input()
8080
get value(): MyTel | null {
81-
const {value: {area, exchange, subscriber}} = this.parts;
82-
if (area.length === 3 && exchange.length === 3 && subscriber.length === 4) {
81+
if (this.parts.valid) {
82+
const {value: {area, exchange, subscriber}} = this.parts;
8383
return new MyTel(area, exchange, subscriber);
8484
}
8585
return null;
@@ -97,9 +97,9 @@ export class MyTelInput implements ControlValueAccessor, MatFormFieldControl<MyT
9797
@Optional() @Self() public ngControl: NgControl) {
9898

9999
this.parts = formBuilder.group({
100-
area: '',
101-
exchange: '',
102-
subscriber: '',
100+
area: [null, [Validators.required, Validators.minLength(3), Validators.maxLength(3)]],
101+
exchange: [null, [Validators.required, Validators.minLength(3), Validators.maxLength(3)]],
102+
subscriber: [null, [Validators.required, Validators.minLength(4), Validators.maxLength(4)]],
103103
});
104104

105105
_focusMonitor.monitor(_elementRef, true).subscribe(origin => {

0 commit comments

Comments
 (0)