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

Form is marked dirty when dropdown is pre-populated #39

Open
adulfan opened this issue Feb 28, 2024 · 6 comments
Open

Form is marked dirty when dropdown is pre-populated #39

adulfan opened this issue Feb 28, 2024 · 6 comments

Comments

@adulfan
Copy link

adulfan commented Feb 28, 2024

I have a dropdown that get's pre-populated with default first value from the options, when the form is created it's empty all values are null but then inputs$ that I am using from ngx-sub-form on subscribe are populated with the dropdown values so the form becomes pristine:false and the form is marked as dirty, I am not clear how to fix it. Thank you


initControlWithOptions() {
    if (this.autoDisplayFirst && this.options && this.options.length > 0 && this.control) {
      const firstOptionValue = this.optionValue ? this.options[0][this.optionValue] : this.options[0];
      this.control.patchValue(firstOptionValue, { emitEvent: true });
    }
  }

Angular version: 17
 
For Tooling issues:
- Node version: v18.17.0  
- Platform:  Mac 
@NetanelBasal
Copy link
Member

Both the form and the source should be the same value, so you probably should update also the source value.

@adulfan
Copy link
Author

adulfan commented Feb 28, 2024

I am not clear where? I an checking onInit
this.sub = dirtyCheck(this.form.formGroup, this.input$,
{ debounce: 100, withDisabled: true, useBeforeunloadEvent: false })
.pipe(takeUntil(this.onDestroy$))
.subscribe(isDirty => {
this.formStateService.updateDirtyFlag(isDirty);
console.log('from the root form', isDirty);
});
and isDirty is true because values in this.input$ different then this.form.formGroup, that's what i can'e figure out where to test the values?

@NetanelBasal
Copy link
Member

Can you create a simple reproduction on stackblitz?

@adulfan
Copy link
Author

adulfan commented Feb 28, 2024

I have never tried it, but basically if the form has some initial values that were passed into dropdowns the inputs$ are updated with the values and the formGroup is not, so I am not clear at what point to check for dirty, I also installed and downloaded the git but it's broken.

@adulfan
Copy link
Author

adulfan commented Feb 28, 2024

https://angular-6-template-1rcxch.stackblitz.io see this please
when I print out formValues and Inuts
form {"firstName":"","lastName":"","draftQuality":"NY","address":{"street":"","city":"","state":"","zipCode":""},"emails":[{"email":""}]}
input$ {"firstName":"","lastName":"","draftQuality":"","address":{"street":"","city":"","state":"","zipCode":""},"emails":[{"email":""}]}
inputs are not updated with initial values.

@adulfan
Copy link
Author

adulfan commented Feb 29, 2024

Hello, where you able to see the code?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants