store icon indicating copy to clipboard operation
store copied to clipboard

🐞[BUG]: @ngxs/form-plugin and FormArray

Open AlgoTrader opened this issue 8 years ago • 5 comments

I have a model like this:

{model: {items: []}, dirty: ...,}

then I add an item to items and I expect FormArray to get a new FormGroup/FormControl item also.

The forms with FormControl/FormGroup are usually static. The game is changed completly when FormArray is coming, form becomes dynamic with rows coming and leaving

In this case, I have to append a row to a FormArray, put apropriate data to controls and force syncing FormArray to model. I cannot change form changing ngxs model, I have to modify the underlying FormArray, put values there

AlgoTrader avatar Apr 10 '18 08:04 AlgoTrader

I do the strange thing:

// append a new row to items FormArray
this.form.items.push(new this.fb.group({firstName:". lastName: ''}));

// as soon as row is "prepared" it is safe now to update a store
this.store.dispatch(new AppendNewItem({firstName: 'John', lastName: 'Smith'}))

AlgoTrader avatar Apr 10 '18 08:04 AlgoTrader

I've played a bit with FormArray and it's working ok for simple cases where you have a FormArray with a bunch of check boxes. Of course you have to first create the FormGroup as just adding an array in the state/model won't create the UI controls.

tsawan avatar Apr 13 '18 14:04 tsawan

Yep, I need to update the state model and the form model in sync.

In some form lib I seen the following solution - for every form array there is the factory function provided that generate 'empty' array item

Anyway, for dynamic forms the ngxs forms plugin is of little help.

AlgoTrader avatar Apr 13 '18 14:04 AlgoTrader

if you can share a screenshot of your dynamic form, that may help?

tsawan avatar Apr 13 '18 15:04 tsawan

I just had the same issue. What I did was initializing the form only AFTER I got the state data.

Demo -https://stackblitz.com/edit/angular-5xyj9d?file=src%2Fapp%2Fapp.component.html

Newbie012 avatar Sep 27 '18 07:09 Newbie012