joanpablo / reactive_forms

This is a model-driven approach to handling form inputs and validations, heavily inspired in Angular's Reactive Forms
MIT License
472 stars 90 forks source link

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

Open yeikel16 opened 9 months ago

yeikel16 commented 9 months ago

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

joanpablo commented 8 months ago

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 commented 8 months ago

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 commented 8 months ago

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 commented 3 months ago

Hey @joanpablo any chance for this to be reviewd?