6

My expected results is to reset the form after it has been submit and set its default values binded to the formGroup control. I am attempt to reset the form to its default data after submitting by calling reset() on the form submit. Please advise me on how can I reset the default value in the date and time field .

Example :

pickupDate = new Date().toISOString().slice(0,10);
pickupTime = moment().format() ;

onSubmit(orderData: Order){
        this.apiService(orderData).subscribe( order => {
                 orderForm.reset()
})
}

Please help Thanks

6
  • You have to set the model properties to empty strings. Clearing the form won't do what you expect. Commented Apr 17, 2018 at 5:07
  • You can patchValues to the form or re-build the form after resetting. Commented Apr 17, 2018 at 5:09
  • 1
    @RandyCasburn resetting the form fields to empty string would not be efficient to add to it my form group has a allot of fields. Commented Apr 17, 2018 at 14:24
  • @AnuradhaGunasekara can you be more clear on how I can achieve this using patchValue. Commented Apr 17, 2018 at 14:25
  • my formgroup looks similar to this: this.orderForm = fb.group({ pickupDate: [this.pickupDate, Validators.required], pickupTime: [this.pickupTime, Validators.required] }) Commented Apr 17, 2018 at 14:25

5 Answers 5

12

After submitting your form. you are calling

this.yourForm.reset()

Then you can patch initial values to the form like this.

this.yourForm.patchValue({
  firstControllerName: this.initialValues.value1,
  secondControllerName: this.initialValues.value2,
  // other controller names goes here
});

Hope this helps.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I reinitialize the form after calling reset
4

I don't know how your formBuilder looks like, but

this.yourForm.reset({}) already does the trick to empty fields that weren't predetermined. You could attempt something similar to this...

  ngOnChanges() {
    this.queryForm.reset({
    });

    // We generate an empty form again. 
    this.createForm();
  }

And your createForm could look like this...

createForm() {

    // As the method name suggests, we create a form. A form consists of Groups, Controls - and optionally - Arrays...
    this.queryForm = new FormGroup({
      query: new FormControl(''),

      formArray: new FormArray([this.initQueryConditions())
    })
  }

Comments

3

You can reset and set the default values you want at the same time like this:

    defaultFormValues: any = {
        date: Date.now
    };

    formG: FormGroup = new FormGroup({
        'date': new FormControl(this.defaultFormValues.date)
    });

    onSubmit(orderData: Order) {
        this.apiService(orderData).subscribe(order => {
            this.formG.reset(this.defaultFormValues);
        });
    }

Comments

0

Approach for clearing depends whether you are using Template driven forms or Reactive forms.

Angular Forms, check full syntax with explaination

If template, then you need to set the model object to a default one (not necessarily empty). If reactive, then call a method which explictly empties each of form's value.

After that, do the form.Reset to move it back to "pristine | unchanged" state, thus removing validators as well

Comments

0

What I have found convenient is to segregate the form creation into a separate function. This way you can always reset the whole form just by calling the function.

  public form: FormGroup;
  private createForm(): void {
    this.form = this.builder.group({
      title: [ '', [Validators.required] ],
      description: [ '', [Validators.required, Validators.maxLength(200)] ],
      category: [ '', [Validators.required] ]
    });
  }

  private onSubmit(data) {
    this.apiService(data).subscribe(order => {
      this.createForm();
    });
  }

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.