Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Form to Pristine without clearing data

I have a form that displays a list of <input type="text"> elements. They all share a common save button which is disabled until the form becomes dirty. Then, once the user clicks the save button, the data will be submitted to the server. If the server successfully saved the data, I want to reset the form to a pristine state, but I want to keep all of the data in the form so the user can edit the data further if they so choose.

After searching, I have found the NgForm.reset() method. While this does set the form to pristine, it unfortunately also clears out the form. The reset method does seem to have a value parameter, but I can't seem to find out what it does. Nevertheless, I don't want the data cleared out.

I have also tried myForm.pristine = true as well, but this causes a reload of the page for some reason.

Here is a plunkr demonstrating the problem.

like image 572
spectacularbob Avatar asked Nov 19 '16 07:11

spectacularbob


People also ask

How do you set a pristine form?

$setPristine(); Sets the form to its pristine state. This method sets the form's $pristine state to true, the $dirty state to false, removes the ng-dirty class and adds the ng-pristine class. Additionally, it sets the $submitted state to false.

How do you make a pristine false form?

Using $setPristine will set the $pristine property of the form to true and $dirty to false. $setDirty will do the contrary.

What's the difference between dirty touched and pristine on a form element?

pristine: This property returns true if the element's contents have not been changed. dirty: This property returns true if the element's contents have been changed. untouched: This property returns true if the user has not visited the element. touched: This property returns true if the user has visited the element.

What is the best way to clear all inputs in a Ngform?

In a model-driven form to reset the form we just need to call the function reset() on our myform model. The form now resets, all the input fields go back to their initial state and any valid , touched or dirty properties are also reset to their starting values.


2 Answers

What you're looking for is myForm.form.markAsPristine().

like image 180
Mohamed Fakhreddine Avatar answered Nov 08 '22 10:11

Mohamed Fakhreddine


At the moment I can suggest two possible solutions. First one is really close to what you've suggested, as form's reset method has following signature and accepts form value as a first argument:

//@angular/forms/src/model.d.ts: reset(value?: any, {onlySelf}?: { onlySelf?: boolean; }): void; 

In the submit handler, we will capture a copy of the last state:

const { myForm: { value: formValueSnap } } = this; 

And do the reset itself:

this.myForm.reset(formValueSnap, false); 

Another option from the times, when there was no possibility to reset form, is to create a helper method, which will mark each control as pristine and will keep the data. It can be called in the same submit helper in place of the resetting.

private _markFormPristine(form: FormGroup | NgForm): void {     Object.keys(form.controls).forEach(control => {         form.controls[control].markAsPristine();     }); } 

Link to the updated plunkr.

like image 39
Daniel Mylian Avatar answered Nov 08 '22 11:11

Daniel Mylian