ngx-sub-form icon indicating copy to clipboard operation
ngx-sub-form copied to clipboard

Default form values do not show

Open ophirKatz opened this issue 3 years ago • 6 comments

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.

ophirKatz avatar Nov 23 '22 12:11 ophirKatz

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

maxime1992 avatar Nov 23 '22 16:11 maxime1992

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?

ophirKatz avatar Nov 24 '22 13:11 ophirKatz

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

maxime1992 avatar Nov 29 '22 09:11 maxime1992

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.

ophirKatz avatar Nov 29 '22 16:11 ophirKatz

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

maxime1992 avatar Nov 30 '22 14:11 maxime1992

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

ophirKatz avatar Nov 30 '22 14:11 ophirKatz