35

Is there a way to reset a reactive form group into its initial values instead of get it empty using .reset() method ?

I have the following stackblitz, where the default selected value is Parent, and if the user change it to sister, and wants to get the form to initial value, click on reset button to do so.

Currently, I tried:

this.formGroup.reset();
//Or
this.formGroup.markAsPristine;
//Or
this.formGroup.markAsTouched

The .reset() reset the form completly.

P.S. Some people may say that the user can re-select the default value instead of clicking on reset button. But in my case, I have a huge user form where he needs to update his information, and he may needs to reset to initial values if he was mistaken in some values.

3
  • Why not adding this.formGroup.get('family_relation').setValue(1) at the end of reset() method? Commented Dec 10, 2018 at 13:51
  • It is a form control array with several rows and multiple form control. I think it is not a good idea to set manually the value of each of them Commented Dec 10, 2018 at 13:52
  • As of Angular 13 you can do this: stackoverflow.com/a/70461013/651555 Commented Jan 12, 2022 at 10:03

6 Answers 6

61

You can save the form initial values:

this.initialValues = this.formGroup.value;

And then pass those values to the reset function:

this.formGroup.reset(this.initialValues);
Sign up to request clarification or add additional context in comments.

Comments

14

Tou should make control nonNullable // If this flag is set, the control will instead reset to the initial value.

const cat = new FormControl('tabby', {nonNullable: true});
cat.reset(); 

cat.value is "tabby"

2 Comments

This is the correct answer. Just setting it as nonNullable will reset it to the initial value that was set when the form was created.
@AhmadAlfy That's an implementation with strictly typed forms which is available from Angular 14 and not Angular 7 like the question asked for.
7

Please find the stackbliz here

https://stackblitz.com/edit/angular-material-ciztu9

Angular Provides reset(formState:any = null), you can pass the initial formState/object as a first parameter

For more info https://angular.io/api/forms/FormControl#reset

Comments

3

Create a method setForm() which will be called in the constructor and reset()

setForm() {
  this.formGroup = this.fb.group({
    'family_relation': new FormControl(this.familyRelationArray[0]['family_relation_id'])
  });
}

(Remove the code from the constructor which set the initial values)

resetForm() now looks like:

reset() {
    this.formGroup.reset();
    //Or
    this.formGroup.markAsPristine;
    //Or
    this.formGroup.markAsTouched

    this.setForm();
  }

And the constructor:

constructor(private fb: FormBuilder) {

    this.familyRelationArray = [
      {
        family_relation_id: 1,
        family_relation_type: 'Parent'
      },
      {
        family_relation_id: 2,
        family_relation_type: 'Sister'
      }
    ];
    this.setForm();

  }

4 Comments

It is better to use inside ngOnInit ?
Sure! then take the this.setForm() from the c'tor and place in ngOninit() voila
It is a form control array with several rows and multiple form control. I think it is not a good idea to set manually the value of each of them
Well you will have to if you want to use the "initial" default value
2

Each FormControl has a readonly property defaultValue that will be used to reset the value when the control is being reset without a explicit value.

Normally when creating a new FormControl the defaultValue will be set to null but when passing { nonNullable: true } as the options argument the defaultValue will be set to the initial value you pass to the control. Calling reset without an argument will now reset the control to the initial value.

const value = 'my initial value';
const formControl = new FormControl(value, { nonNullable: true });

formControl.reset(); // Resetting to initial value.

Note:

See also the comment inside the FormControl class at the defaultValue:

/**
 * The default value of this FormControl, used whenever the control is reset without an explicit
 * value. See {@link FormControlOptions#nonNullable} for more information on configuring
 * a default value.
 */
readonly defaultValue: TValue;

More information from the FormControlOptions at the nonNullable property:

/**
 * @description
 * Whether to use the initial value used to construct the `FormControl` as its default value
 * as well. If this option is false or not provided, the default value of a FormControl is `null`.
 * When a FormControl is reset without an explicit value, its value reverts to
 * its default value.
 */
nonNullable?: boolean;

Comments

1

There might be other ways of doing it, but I would do it using one of following methods:

Method 1: Passing values to the reset method.

reset() {
  this.formGroup.reset({family_relation: 1});
  //Or
  this.formGroup.markAsPristine;
  //Or
  this.formGroup.markAsTouched;
}

Method 2: Setting values

reset() {
  this.formGroup.reset();
  //Or
  this.formGroup.markAsPristine;
  //Or
  this.formGroup.markAsTouched;
  // this.formGroup.get('family_relation').setValue(1)
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.