0

In angular 5 I have a form with user can add multiple records of charges which can save at once. after save need to remove those records and go back to initial state with one row. I have tried as following code it goes to initial state but the validation is firing when go back to that state.

ngOnInit() {
    this.fastPostingForm = this.fb.group({
      Charges: this.fb.array([
        this.initCharge()
      ])
    });
}

initCharge() {
    return this.fb.group({
      room: ['', Validators.required],
      transactionCode: ['', Validators.required],
      amount: ['', Validators.required],
      quantity: [1, [Validators.required, Validators.min(1), Validators.max(9999)]],
      window: ['', Validators.required],
      reservationId: [''],
      rsv:[{}]
    });
  }

  addCharge() {
    const control = <FormArray>this.fastPostingForm.controls['Charges'];
    control.push(this.initCharge());
    console.log(control.length);
  }

 saveForm(){
        //save process

        //clear form and remove form array elements

        this.fastPostingForm = this.fb.group({
      Charges: this.fb.array([
        this.initCharge()
      ])
    });

  }

In the UI validation is done using following condition for each control.

 <mat-error *ngIf="!fastPostingForm.controls.Charges.controls[i].controls.room.valid && fastPostingForm.controls.Charges.controls[i].controls.room.touched">message
</mat-error

>

4
  • Did you tried this.fastPostingForm.reset();? Commented Dec 13, 2018 at 6:39
  • yes, it does not remove additional rows which were added. Commented Dec 13, 2018 at 6:51
  • But did you try reset() after saving form and removing additional rows? Commented Dec 13, 2018 at 7:04
  • yes, tried. also doesnot work, Commented Dec 13, 2018 at 7:34

2 Answers 2

2

Try this

saveForm(){

 this.fastPostingForm = this.fb.group({
      Charges: this.fb.array([
        this.initCharge()
 ])
 this.fastPostingForm.markAsUntouched();
 this.fastPostingForm.markAsPristine();
 this.fastPostingForm.updateValueAndValidity();

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

3 Comments

try to add this also this.fastPostingForm.markAsPristine();
can you create stackbitz
*ngIf="!fastPostingForm.controls.Charges.controls[i].controls.room.valid && fastPostingForm.controls.Charges.controls[i].controls.room.touched" I have used this condition in UI.
1

You don't need to create the group again. You can simply remove all the elements from the FormArray. Use :

(<FormArray>this.fastPostingForm.controls.Charges).controls.splice(0, (<FormArray>this.fastPostingForm.controls.Charges).controls.length);

then call the addCharge() to add the default first element.

10 Comments

it gives me a error. Cannot invoke an expression whose type lacks a call signature. Type 'AbstractControl' has no compatible call signatures.
My bad, missed the charges controls. You need to get the Charges as a FormArray, then you can splice the controls like any other array.
but how to solve validation firing issue. after calling addcharge() function the input's validation is firing. need to go pack to the initial state.
If the form is displayed on the UI, validators will be fired every time a control is rendered on UI.
but initial loading of the page does not firing any validation messages
|

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.