Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

setObject -- inconsistent behavior on FormArrays #195

Closed
jbjhjm opened this issue Feb 14, 2022 · 3 comments
Closed

setObject -- inconsistent behavior on FormArrays #195

jbjhjm opened this issue Feb 14, 2022 · 3 comments

Comments

@jbjhjm
Copy link

jbjhjm commented Feb 14, 2022

Don't whack me please :'D I think I found another small bug.

setObject seems to handle FormArrays and FormGroups differently:

  • For FormGroups, patched values will be replaced by new values.
  • For FormArrays, new values will be ADDED to patched values.

So let's assume we have a Dynamic FormGroup set up with data like this:

{
  name:'foo',
  attachments:[
    {name:'test',file:'path'},
    {name:'stuff',file:'path2'}
  ]
}

And then run

formGroup.setObject({
  name:'bar',
  attachments:[
    {name:'changed',file:'newpath'},
    {name:'stuff',file:'path2'}
  ]
})

the form value will become

{
  name:'bar',
  attachments:[
    {name:'test',file:'path'},
    {name:'stuff',file:'path2'},
    {name:'changed',file:'newpath'},
    {name:'stuff',file:'path2'}
  ]
}

the naming setObject indicates the whole form value will be replaced, in opposite to something like patchObject.
So I think the current behavior is inconsistent and existing FormArray items should be cleared before adding new controls.

@EndyKaufman
Copy link
Owner

@jbjhjm it's not a bug, it's a feature :) I'll think about how to solve it

@jbjhjm
Copy link
Author

jbjhjm commented Feb 14, 2022

@EndyKaufman ahhh ok I see :) It was a bit unexpected for me.
Logical solutions would be to

  • add a patchObject method or
  • allowing configuration like setObject(data,{patchArrays:true}).

For now I'll iterate the root formGroup and clear array entries before repatching.
This could be packed into a method like resetAllFormArrays.
Then however, the behavior of setObject should be documented accordingly so that noone stumbles upon this.

EndyKaufman added a commit that referenced this issue Feb 19, 2022
fix: setObject -- inconsistent behavior on FormArrays (#195)
@EndyKaufman
Copy link
Owner

@jbjhjm solved in new version https://github.com/EndyKaufman/ngx-dynamic-form-builder/releases/tag/v2.2.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants