question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

docs-bug(Guide: Creating a custom form field control): Missing Validators in FormGroup

See original GitHub issue

Documentation 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:closed
  • Created 4 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
mmalerbacommented, Mar 14, 2020

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 one

0reactions
angular-automatic-lock-bot[bot]commented, Apr 16, 2020

This 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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found