Skip to content

bug(forms): Value and status are out of sync in ngModelChange callback #21513

@ppham27

Description

@ppham27

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

Within the ngModelChange callback, the form validity doesn't reflect the control status. For example, if the value fails validation, the form control will still have VALID status.

Expected behavior

If the value fails validation, I would expect INVALID status.

Minimal reproduction of the problem with instructions

Consider the component:

<input #ngModel="ngModel" ngModel [maxlength]="4"
       (ngModelChange)="onNgModelChange(ngModel)">

where onNgModelChange is defined:

class MyComponent {
  onNgModelChange(ngModel: NgModel) {
    console.log(ngModel.value);
    console.log(ngModel.status);
  }
}

If you the value goes from valid to invalid, VALID will be logged despite value being INVALID. When using async validators, one would expect PENDING to be logged, but the previous status is logged instead.

See #21514 for a test case.

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

I would like to filter out invalid values in the callback. As an aside, this was easier in AngularJS, where one could set allowInvalid to falsein ngModelOptions.

Environment


Angular version: 5.2.0


Browser:
- [x] Chrome (desktop) version 64
- [x] Chrome (Android) version XX
- [x] Chrome (iOS) version XX
- [x] Firefox version XX
- [x] Safari (desktop) version XX
- [x] Safari (iOS) version XX
- [x] IE version XX
- [x] Edge version XX
 
For Tooling issues:
- Node version: 8.9.4  
- Platform:  Mac, Linux, Windows

Others:

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions