question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Form Status Changes observable, should also emit the Value used for validation.

See original GitHub issue

Which @angular/* package(s) are relevant/releated to the feature request?

forms

Description

We have a requirement to make a call to API every time the value of form is changed, we need to make an API call, if it is valid. Form is a complex form with multiple child groups and child arrays.

Since valueChanges is fired before statusChanges and since statusChanges can be async; it is not possible to do this in a reliable way.

Proposed solution

statusChanges event should also emit the value used for calculating the status.

Alternatives considered

To solve this issue, I am using the following observable. This is not ideal, however I could not think of a better way.

const filterSetValidityChanged$ = this.searchForm.get('filterSet').statusChanges.pipe();
const filterSetChanged$ = this.searchForm.get('filterSet').valueChanges.pipe();

const filterSetChangedAndIsValid$ = filterSetValidityChanged$.pipe(
  filter(a => a === 'VALID'),
  withLatestFrom(filterSetChanged$),
  map(([, value]) => value));

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:4
  • Comments:11 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
dylhunncommented, Mar 29, 2022

Why do you need withLatestFrom? It seems to me that you could write a little helper function that does the job. Like:

function validValues(control: AbstractControl) {
  return control.statusChanges.pipe(
    filter((a) => a === 'VALID'),
    map(() => control.value),
  );
}

I think the issue is that the value might have changed in the meantime, since the validation is async and could take a while.

1reaction
dylhunncommented, Mar 29, 2022

This might be solvable in the context of #10887, if we had a unified event payload that contained both the value and status for all state changes.

Read more comments on GitHub >

github_iconTop Results From Across the Web

StatusChanges in Angular Forms - TekTutorialsHub
StatusChanges event raised by the Angular forms whenever the it calculates validation status of Form. Returns an observable so that you can ......
Read more >
valueChanges and statusChanges in Angular
valuesChanges() Property is an observable that emits an event every time the value of a FormGroup or FormControl changes. valueChanges() ...
Read more >
Angular valueChanges and statusChanges - ConcretePage.com
This page will walk through Angular valueChanges and statusChanges properties of FormControl , FormArray and FormGroup classes.
Read more >
Angular reactive forms: how show validation errors in a dialog?
valueChanges: Observable --> emits an event every time the value of the control changes ... Here statusChanges Observable can also be used.
Read more >
Value & Status Change events | Reactive Forms | Angular 13+
In this lecture you are going to learn about value and status change events in Angular Form. These are the events which gets...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found