Skip to content

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

Closed
@tobiasschweizer

Description

@tobiasschweizer

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsThis issue is related to documentation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions