Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

validatorChange triggers valueChanges emit even if model stays the same #25749

Open
pkozlowski-opensource opened this issue Aug 30, 2018 · 3 comments
Labels
area: forms breaking changes freq2: medium P4 A relatively minor issue that is not relevant to core functions state: confirmed type: bug/fix
Milestone

Comments

@pkozlowski-opensource
Copy link
Member

I'm submitting a...


[x] Bug report  

Current behavior

When a control is implementing the Validator interface and informs forms infrastructure about validator condition changes (through registerOnValidatorChange) the valueChanges is emitting each time validator change is called, even if model stays the same.

Expected behavior

valueChanges should emit only if the underlying model value changes, regardless of the validation changes.

Minimal reproduction of the problem with instructions

https://stackblitz.com/edit/angular-value-change-e79u33?file=app%2Fapp.component.ts

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

valueChanges should not emit when the model is not changing - otherwise its basic contract is broken.

Environment


Angular version: ^6.0.0


Browser: ALL
@kara
Copy link
Contributor

kara commented Aug 30, 2018

@pkozlowski-opensource The valueChanges observable actually emits whenever the value is re-calculated, not necessarily when it changes (the same is true for statusChanges). If you call setValue() with the same value 10 times, you will get 10 valueChanges events. The observable's name is a bit misleading, but this is how it's intended to work. If you need only new values, you can add a distinctUntilChanged operator.

That said, it doesn't seem necessary to re-calculate the value when the validator changes. We only technically need to re-calculate the validation status, so perhaps that would be the way forward here (though breaking).

@popec
Copy link

popec commented May 27, 2020

Regarding #18574
How can I emit valueChanges on formControl programatically for same value now ?

@jelbourn jelbourn added P4 A relatively minor issue that is not relevant to core functions and removed severity1: confusing labels Oct 1, 2020
@csvn
Copy link
Contributor

csvn commented Sep 9, 2021

This seems to be the root cause for angular/components#9295, where changing the min validator on MatDateRangeInput causes two new valueChanges to emit, when just changing the validation. The linked issue can likely not be solved without first fixing this one. Could it maybe be included for v13?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: forms breaking changes freq2: medium P4 A relatively minor issue that is not relevant to core functions state: confirmed type: bug/fix
Projects
None yet
Development

No branches or pull requests

6 participants