Ranjith Varatharajan
Ranjith Varatharajan

Reputation: 1694

Two forms with same FormGroup?

Is it possible to create two forms with same FormGroup?

I have one form to add an entity and another to edit. both forms have same form elements. So far i have to write two form groups and access the data. But i need to make the formgroup global and use that in both forms. Is it possible? Please point me in a right direction. Any advice would be helpful. Thank you.

Here is my snippet:

AddEntityComponent:

this.addEntityForm = addEntityFormBuilder.group({
      'Code': "",
      'Name': [null, Validators.required],
      'Type': "",
      'Email': "",
      'Website': "",
      'DefaultDivision': "",
      'EffectDate': Date.now,
      'PanNo': "",
      'HomeCurrency': "",
      'LstNo': "",
      'CstNo': "",
      'FaxNo': "",
      'Address1': "",
      'Address2': "",
      'Address3': "",
      'RegdAddress1': "",
      'RegdAddress2': "",
      'RegdAddress3': "",
      'IsActive': ""
    });

Need to use this formgroup in both addentitycomponent and editentitycomponent.

Upvotes: 2

Views: 3361

Answers (1)

Rajesh Sivasankaran
Rajesh Sivasankaran

Reputation: 136

        if(flag="add")
        {
            this.addEntityForm = addEntityFormBuilder.group({
              'Code': "",
              'Name': [null, Validators.required],
              'Type': "",
              'Email': "",
              'Website': "",
              'DefaultDivision': "",
              'EffectDate': Date.now,
              'PanNo': "",
              'HomeCurrency': "",
              'LstNo': "",
              'CstNo': "",
              'FaxNo': "",
              'Address1': "",
              'Address2': "",
              'Address3': "",
              'RegdAddress1': "",
              'RegdAddress2': "",
              'RegdAddress3': "",
              'IsActive': ""
            });
        }

        elseif(flag="edit")
        {
            var entity=service.getentity();
            this.addEntityForm = addEntityFormBuilder.group({
              'Code': entity.code,
              'Name': entity.name,
               ....
              'IsActive': entity.IsActive
            });
        }


    It assigns data when edit.
same when submit the form use flag.

Upvotes: 1

Related Questions