Ngx-formly: Multiple instances of formly-form on the same page are overlapping

Created on 14 Mar 2019  路  24Comments  路  Source: ngx-formly/ngx-formly

I'm submitting a ... (check one with "x")

[x] bug report => search github for a similar issue or PR before submitting
[ ] feature request
[x] support request

When placing multiple instances of formly-form on the same page, the fields overlap and only the latest wins.

This could be happening due to the fact that Angular re-uses components and hence I am getting this odd behavior.

I've put down a sample that you can check and assess.

https://stackblitz.com/edit/angular-xjh3ep

Thank you
Bilal

question

All 24 comments

that's a mutability issue, we've introduced immutable mode in order to solve that common issue.

So you've several ways to solve that:

@NgModule({
  imports: [
    FormlyModule.forRoot({
      extras: { immutable: true }
    }),
  ],
})

Thanks @aitboudad
When using immutable I cannot find the resetModel() function on the options object anymore!

When using the first option, it works fine for me. It happens that I have a loop and in each iteration, I render a form. Now, every form component receives its own copy of the fields.

Now the problem is that fields become read-only. Check again the sample after I deep copied the fields:

https://stackblitz.com/edit/angular-xjh3ep

Thanks

Now the problem is that fields become read-only.

calling function that return a new object triggers a change detection infinitely, the solution is the copy fields see https://stackblitz.com/edit/angular-xjh3ep

When using immutable I cannot find the resetModel() function on the options object anymore!

    <form [formGroup]="form" (ngSubmit)="submit()">
      <formly-form #formlyForm ...></formly-form>
      <button type="button" (click)="formlyForm.options.resetModel()">Reset</button>
    </form>

@aitboudad I guess you didn't update the sample above! I still see the old one.

@aitboudad a kind reminder

So basically, when rendering a form with ngIf I need to have a fields property per item form? I just cannot use the same fields property all over for each iteration. Is that what I understand?

with ngIf it should work, but rendering the same field in different places may produce some unexpected behavior.

In fact, my scenario is rendering multiple records in forms. So each form is using the same component, but, the model differs from one form to another.

but the passed fields input is shared which and each field is responsible to render one component type

@aitboudad
True, that's why now what I've done is for each model I push to an array the fields. So I will have an array of the same length of the array of models, each item in this array has a copy of the fields.

Now, I am left with one issue. Since Angular re-uses, the same instance of my Custom Form Wrapper, any state inside the component is not being dealt with it properly. Is there a way to force Angular to either create a new instance of a component inside ngFor or come up with isolation for the state inside the Custom Form Wrapper component?

Since Angular re-uses, the same instance of my Custom Form Wrapper

not true

I am not sure we are talking about the same Form Wrapper. It's a component I've created to wrap the formly form. I noticed that the same ID is given for the components which made me think they are being re-used.

Notice here. Two instances of my form components are being rendered. Both get the same id.

image

sorry that's out of my scope :)

Hello, I have same problem.
Is there any solution to solve this?

I've already tried that. But the problem is, I need to use ngFor for iterating several formly-form.
So now I have the same issue @bhaidar has.

which problem, can you provide a reproduction example using stackblitz?

https://stackblitz.com/edit/angular-hnekxc

plz check here. There's two wizards separated by tabs.
You can change any form fields, but after submit, you can see two models of each form are same.
This is the problem.

@top-dev830 for each wizard pass a separate form instance similar to the model

can you plz updateh the stackblitz?

Thanks. It's working very well.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bhaidar picture bhaidar  路  28Comments

vishaalsharmaa picture vishaalsharmaa  路  25Comments

Suraj786 picture Suraj786  路  16Comments

smile2014 picture smile2014  路  19Comments

bejgumshirisha picture bejgumshirisha  路  19Comments