docs-bug(Guide: Creating a custom form field control): Missing Validators in FormGroup
See original GitHub issueDocumentation Feedback
In the example implementation of a MatFormFieldControl
FormGroup
is used. However, validation happens in the value getter:
@Input()
get value(): MyTel | null {
const {value: {area, exchange, subscriber}} = this.parts;
if (area.length === 3 && exchange.length === 3 && subscriber.length === 4) {
return new MyTel(area, exchange, subscriber);
}
return null;
}
I think it would be better if a validator was provided. Then in the value getter, simply the validity of the form would have to be checked:
@Input()
get value(): MyTel | null {
if (this.parts.valid) {
const {value: {area, exchange, subscriber}} = this.parts;
return new MyTel(area, exchange, subscriber);
}
return null;
}
I guess Validators.minLength()
and Validators.maxLength()
would do the job.
Affected documentation page: https://material.angular.io/guide/creating-a-custom-form-field-control
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (5 by maintainers)
Top Results From Across the Web
Angular Custom Form Controls - Complete Guide
Build a fully functional custom form control, compatible with template-driven and reactive forms, as well as with all built-in and custom ...
Read more >Creating a custom form field control - Angular Material
Here is what we'll build by the end of this guide: Form field with custom telephone number input control. link
Read more >Angular2 Custom Form Validators losing access to `this` in class
As such, I'm using validator functions on the input fields, including custom validators. This works fine. My issue is that I am also...
Read more >How To Use Custom Form Validation in Angular - DigitalOcean
In this tutorial, you will construct a custom validator for a phone number input field in an Angular application.
Read more >Building Custom Validators in Angular - Pluralsight
Custom validators take the value from the FormControl , where every input acts as a FormControl . So let's create a form along...
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 FreeTop 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
Top GitHub Comments
Sure, just run
yarn dev-app
and go to localhost:4200/examples. There’s a lot of examples, so ctrl+f for “form-field-custom-control” and expand that oneThis issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.