Angular: Form validators re-run on view (re)creation

Created on 28 Sep 2017  ·  1Comment  ·  Source: angular/angular

I'm submitting a...


[ ] Regression (a behavior that used to work and stopped working in a new release)
[x] Bug report  
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior

When the directive associated with a form control is created in the view, validators are triggered.

Expected behavior

Validators should not be triggered when the view changes. Reactive forms should not be tied to the way they are represented in the view. Their source of truth should be the model.

Minimal reproduction of the problem with instructions

https://stackblitz.com/edit/angular-reactive-forms-issue?file=app/app.component.ts

  1. Open console.
  2. Use the _Toggle visiblity_ button to confirm that validators are running on each control directive creation.

What is the motivation / use case for changing the behavior?

  1. Continue from above steps.
  2. Press _set errors manually_ to insert an error manually (this is mocking manually setting an error after validation is returned from backend)
  3. Press _Toggle visiblity_ to make it disappear. Nothing happens in console.
  4. Press it again to make the control appear. Notice that console logs, which means that validators is running.

Observe the fact that error message is now gone. Toggling the view is mocking the use-case of having a mulitpart form.


If this is by design, I don't see a reason for it. We're creating a view where we write data form model to view. The action of reading from model should not trigger validators, as far as I understand. As I see it, validators should be run when the FormControl instances are created in the model. _Why are validators triggered when value did not change?_

Environment


Angular version: 4.3.6

P4 forms validators medium confirmed triage #1 bufix

Most helpful comment

Yes sounds right.
A change in the view should not affect the model in reactive forms and no change on the model shouldn't trigger validation.

I've pin point it to https://github.com/angular/angular/blob/17eaef0311990e1a06c028f19bc6c63dcd775f31/packages/forms/src/directives/reactive_directives/form_group_directive.ts#L98-L104

It's always running a new validation run (updateValueAndValidity()), while it only should do it if there was new validators introduced by the control directive.

Also renaming the function into addControlDirective() may reduce confusion on what it's doing.

May be somehow related to #14376 has it's both about editing the view on a reactive form and looks like FormGroupDirective is at fault.

>All comments

Yes sounds right.
A change in the view should not affect the model in reactive forms and no change on the model shouldn't trigger validation.

I've pin point it to https://github.com/angular/angular/blob/17eaef0311990e1a06c028f19bc6c63dcd775f31/packages/forms/src/directives/reactive_directives/form_group_directive.ts#L98-L104

It's always running a new validation run (updateValueAndValidity()), while it only should do it if there was new validators introduced by the control directive.

Also renaming the function into addControlDirective() may reduce confusion on what it's doing.

May be somehow related to #14376 has it's both about editing the view on a reactive form and looks like FormGroupDirective is at fault.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

alexeagle picture alexeagle  ·  105Comments

michalstepien picture michalstepien  ·  113Comments

vicb picture vicb  ·  122Comments

jamesmfriedman picture jamesmfriedman  ·  114Comments

arpit-agarwal picture arpit-agarwal  ·  111Comments