-
Notifications
You must be signed in to change notification settings - Fork 26.7k
Description
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?