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

Add a way to set new default values to reactive form controls #54022

Open
DerChris173 opened this issue Jan 22, 2024 · 7 comments
Open

Add a way to set new default values to reactive form controls #54022

DerChris173 opened this issue Jan 22, 2024 · 7 comments
Labels
area: forms feature: under consideration Feature request for which voting has completed and the request is now under consideration feature Issue that requests a new feature
Milestone

Comments

@DerChris173
Copy link

DerChris173 commented Jan 22, 2024

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

forms

Description

For typical CRUD operations, it's often required to

  • reset the dialog to its initial state
  • query if a particular FormControl has a value that's different than the original one.

The defaultValue/reset() Feature is quite suitable for this. Unfortunately, there's no way to change the defaultValue, e.g. once the user performs a "Save action".

Proposed solution

I propose to enhance the Reactive FormControl API with the following methods:

  • setDefaultValue(newDefaultValue: TValue): Updates the defaultValue of the FormControl to newDefaultValue. Calls to the reset() operation in particular will use this new defaultValue.
  • setCurrentValuesAsDefault(): Convenience method for formControl.setDefaultValue(formControl.getRawValue()).

I'd be happy to develop and raise a PR for this feature.

Alternatives considered

Another alternative would be to build entirely new FormControls if new default values are required. However, this might be less convenient and has issues currently with 2-way-data-binding, I believe.

@DerChris173
Copy link
Author

Thank you for looking into this issue!
I wasn't very clear when I wrote the this issue, but I'm talking about reactive forms, where ngModel is not used.

@DerChris173 DerChris173 changed the title Add a way to set new default values to form controls Add a way to set new default values to reactive form controls Jan 23, 2024
@ngbot ngbot bot added this to the needsTriage milestone Jan 23, 2024
@alxhub alxhub added the feature Issue that requests a new feature label Jan 24, 2024
@ngbot ngbot bot modified the milestones: needsTriage, Backlog Jan 24, 2024
@danieljancar
Copy link
Contributor

Hey @DerChris173,
I agree this feature would be quite beneficial. Below is a simplified use case I came across:

import { FormControl } from '@angular/forms';

class ExampleComponent {
  myFormControl: FormControl;

  constructor() {
    this.myFormControl = new FormControl('initialValue'); // Initial default value
  }

  updateDefaultValue() {
    const newDefaultValue = 'updatedValue';
    this.myFormControl.setDefaultValue(newDefaultValue); // Proposed method to update default value
  }

  resetToDefaultValue() {
    this.myFormControl.reset(); // Resets to 'updatedValue', if updateDefaultValue() was called
  }
}

Let's say a user updates their profile name from 'John Doe' (default) to 'Jane Smith'. Currently, reopening the form for more edits would show 'John Doe' again. The suggested setDefaultValue method would allow the form to show 'Jane Smith' as the default, aligning with the latest data and improving UX.

@DerChris173
Copy link
Author

Hi @alxhub! Thank you for looking at this ticket! I saw that you adjusted the milestone. Does it mean that such a feature would be accepted? I could work on a PR. I already signed the CLA.

@danieljancar
Copy link
Contributor

@DerChris173, this feature request would either require a community vote or the Angular teams internal agreement on this. 😉 I could assist you with developing this feature, if continued.

@angular-robot angular-robot bot added the feature: votes required Feature request which is currently still in the voting phase label Jan 25, 2024
Copy link
Contributor

angular-robot bot commented Jan 25, 2024

This feature request is now candidate for our backlog! In the next phase, the community has 60 days to upvote. If the request receives more than 20 upvotes, we'll move it to our consideration list.

You can find more details about the feature request process in our documentation.

@cluxig
Copy link

cluxig commented Jan 31, 2024

I would prefer an additional option on form.setValue(newValue, true) in case of the form update is used as new default.

@angular-robot angular-robot bot added feature: under consideration Feature request for which voting has completed and the request is now under consideration and removed feature: votes required Feature request which is currently still in the voting phase labels Feb 6, 2024
anliben added a commit to po-ui/po-angular that referenced this issue Mar 4, 2024
Permite emissão de evento com `(p-change-model)` após resetar formulário reativo

Modificação feita por conta de um bug que impedia a emissão do evento após resetar o formulário reativo no Angular.

angular/angular#54022
angular/angular#52135
angular/angular#50971
angular/angular#46458
angular/angular#15741

Fixes DTHFUI-7232
pedrodominguesp pushed a commit to po-ui/po-angular that referenced this issue Mar 4, 2024
Permite emissão de evento com `(p-change-model)` após resetar formulário reativo

Modificação feita por conta de um bug que impedia a emissão do evento após resetar o formulário reativo no Angular.

angular/angular#54022
angular/angular#52135
angular/angular#50971
angular/angular#46458
angular/angular#15741

Fixes DTHFUI-7232
pedrodominguesp pushed a commit to po-ui/po-angular that referenced this issue Mar 4, 2024
Permite emissão de evento com `(p-change-model)` após resetar formulário reativo

Modificação feita por conta de um bug que impedia a emissão do evento após resetar o formulário reativo no Angular.

angular/angular#54022
angular/angular#52135
angular/angular#50971
angular/angular#46458
angular/angular#15741

Fixes DTHFUI-7232
@JeanMeche
Copy link
Member

Without expanding too much the API surface, we could remove the readonly from FormControl.defaultValue to achieve this feature.

Would there be any unwanted side effects if we do this ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: forms feature: under consideration Feature request for which voting has completed and the request is now under consideration feature Issue that requests a new feature
Projects
None yet
Development

No branches or pull requests

7 participants
@pkozlowski-opensource @JeanMeche @alxhub @cluxig @DerChris173 @danieljancar and others