Skip to content

feat(AbstractControlDirective): implement hasError() and getError() #7255

@0x-r4bbit

Description

@0x-r4bbit

Due to #7238 I realised, that when building template-driven forms, we don't have APIs such as hasError() and getError() on control instances.

E.g. having a tiny form like this:

<form>
  <input ngControl="name" #name="ngForm" minlength="3">
  <p *ngIf="name.errors.minlength">Too short!</p>
</form>

We always have to walk down the properties of ngControl instance to access the error type.

However, when this same form is built model-driven, all of a sudden we have additional APIs:

<form [ngFormModel]="someFormModel">
  <input ngControl="name" #name="ngForm" minlength="3">
  <p *ngIf="name.hasError('minlength')">Too short!</p>
</form>

What makes the difference, even though we seem to work with the same instance?

Well, when building model-driven forms, the form model is built in the component and then associated to a form in the DOM using [ngFormModel]. The model consists of Control and ControlGroup and it happens that Control extends AbstractControl which implements hasError() and getError().

We don't have this with template-driven forms, as we're only using the NgControlName directive, which extends NgControl which extends AbstractControlDirective, and that one doesn't implement hasError() or getError().

I'd expect the same APIs in the template no matter if I'm building template-driven forms or model-driven forms. So I propose extending AbstractControlDirective accordingly, as it has access to its Control.

Does that make sense?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions