cloudnc / ngx-sub-form

Utility library for breaking down an Angular form into multiple components
https://cloudnc.github.io/ngx-sub-form
MIT License
315 stars 33 forks source link

Default form values do not show #284

Open ophirKatz opened 2 years ago

ophirKatz commented 2 years ago

Hi :) Trying out the demo app, setting a default value for the FormControl does not appear to affect the UI (the input is empty). Repro: in astromech-droid.component, set toolCount form control to: new UntypedFormControl(10, { validators: [Validators.required] }),. This is the result in the UI (notice the 'Number of tools carried' property is empty): image

Is this behavior OK? How can I set default (and valid) values in the form. The effect, by the way, is that without editing the form by hand, there are no values attached to the form value. Thanks in advance.

maxime1992 commented 2 years ago

Hello,

I just investigated this because indeed, it should definitely behave as you thought it should and have the default value.

It took me a solid 5mn to realize that this is actually only an issue with the old API and that it works as expected with the new API :)

The demo shows by default the old/deprecated API (we really need to remove it soon...). But instead if you go to /rewrite/listings/new you'll that it behaves as expected:

image

ophirKatz commented 2 years ago

Hi, thanks! I'm still having a problem with the form value being null until the form is dirty (in the demo as well), which means that although the default values of the form controls are shown, they are not reflected by the root form's output. Spare your thoughts on this?

maxime1992 commented 1 year ago

Umh I guess it makes sense. Sub forms only broadcast their status if they're valid. It's a tough choice between having sub form values always up to date but possibly in an unwanted state, or always have them in an expected state, meaning that updates on a sub form that makes it invalid aren't propagated up (but the form status should be invalid!).

So you could check if the root form is invalid (or even just the control of the sub form you're working with) and if it's invalid assume that it may be out of date for the value

ophirKatz commented 1 year ago

That's actually not what I meant. The sub-form controls are valid (assume the default values I provided are valid), but the control of the root form that points to the sub-form is null by default (no other logical value because it's the responsibility of the sub-form to fill that out). In this case, the value of the form will not be automatically emitted, and the root form is considered invalid.

maxime1992 commented 1 year ago

Can you provide a step by step repro please? With every possible details you've got. I'm not sure I understand

ophirKatz commented 1 year ago

Yep. I'll work on one next week and share it. Thanks!