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

feat: allow reset the form with the first value his was create #442

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

yeikel16
Copy link

Connection with issue(s)

Close #436

Connected to #382

Solution description

Right now the reset method allow set to null the value of the control, but could not apply to change the initial value of the control when is called. This PR allows you to discard changes made to the form fields.
In some forms it is necessary to update values initially provided, and in some cases it is necessary to reset them with the initial values, for example a user profile where you need to update your personal data.

const pureValue = 'pureValue';

// A control with some default value
final control = FormControl<String>(
    value: pureValue,
    touched: true,
);

// Change the initial value in the control
control.value = 'new value';

// Reset control with the pure value after change the value
control.reset();

// Expect: the control has the initial value after apply the reset
expect(control.value, pureValue);
expect(control.touched, isTrue);

Screenshots or Videos

To Do

  • Check the original issue to confirm it is fully satisfied
  • Add solution description to help guide reviewers
  • Add unit test to verify new or fixed behaviour
  • If apply, add documentation to code properties and package readme

@joanpablo
Copy link
Owner

Hi @yeikel16
Thanks for the PR. I would like to know your opinion about the following question:

Given this code:

const pureValue = 'pureValue';

// A control with some default value
final control = FormControl<String>(
    value: pureValue,
    touched: true,
);

// Change the initial value in the control
control.value = 'new value';

What would be the benefit of:

control.reset()

over the current implementation:

control.reset(value: pureValue)

@yeikel16
Copy link
Author

yeikel16 commented Mar 30, 2024

Given this code:

const pureValue = 'pureValue';

// A control with some default value
final control = FormControl<String>(
    value: pureValue,
    touched: true,
);

// Change the initial value in the control
control.value = 'new value';

What would be the benefit of:

control.reset()

over the current implementation:

control.reset(value: pureValue)

Hello @joanpablo, right now you need to pass the value when resetting the control, forcing it to have the value to be assigned in the reset() function at all times. On the other hand, if nothing is passed, the value of the control remains null, being a bit confusing for the function that it must fulfill according to the name of said reset() function, for this last case I would recommend including a more specific function and descriptive like clean().

With this PR, simply calling the reset() function will take the value with which the control was created and restart it without extra work.

@joanpablo
Copy link
Owner

Hi @yeikel16,

Thanks for the quick response. You are right, I fully understand now the benefits. I agree 100% with you. I would need some time to review this PR thoroughly. It is an important feature and your PR is making important changes in the core of Reactive Forms.

Thanks once again for the contribution and for the patience

@vasilich6107
Copy link
Contributor

Hey @joanpablo any chance for this to be reviewd?

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

Successfully merging this pull request may close these issues.

Reset to default value
3 participants