ngx-formly / ngx-formly

📝 JSON powered / Dynamic forms for Angular
https://formly.dev
MIT License
2.81k stars 566 forks source link

FormState is only observed by last Formly element #3680

Closed GabrielKatz closed 1 year ago

GabrielKatz commented 1 year ago

Version with bug 6.1.5 Reproduction https://stackblitz.com/edit/ngx-formly-ui-material-hlvjcv?file=src%2Fapp%2Fapp.component.ts Expected behavior Toggle button should toggle the top 3 input fields Actual behavior Toggle does nothing

Probable Error Cause:

With the latest change in formly.field.ts (https://github.com/ngx-formly/ngx-formly/pull/3676/files#diff-df0ae785861eb0347b30fc9d957d41cb17da70920e87a746c2b805817412cd42), the form state is being observed on a field level as opposed to the "global" options level. Because the formly observe()-method only works if there is one observation source, this means that only the last field is updated. If that update does not trigger a change to the form group, the change will not propagate.

One approach could be to make the observe()-method work with several sources, like so:

      const { enumerable, set } = Object.getOwnPropertyDescriptor(target, key) || { enumerable: true };
      Object.defineProperty(target, key, {
        enumerable,
        configurable: true,
        get: () => state.value,
        set: (currentValue) => {
          if (currentValue !== state.value) {
            const previousValue = state.value;
            state.value = currentValue;
            set(currentValue)
            state.onChange.forEach((changeFn) => changeFn({ previousValue, currentValue, firstChange: false }));
          }
        },

But i do not know which side-effects such a change might have.

thanecoder commented 1 year ago

Hi @aitboudad , pls suggest if any fix or workaround for this, as the forms are not changing from disabled to enabled state and vice versa.

kenisteward commented 1 year ago

Just double checking did this break on your upgrade to 6 or on upgrade fr 6.1.4 to 6.1.5

On Tue, May 9, 2023, 10:07 PM pbnichat @.***> wrote:

Hi @aitboudad https://github.com/aitboudad , pls suggest if any fix or workaround for this, as the forms are not changing from disabled to enabled state and vice versa.

— Reply to this email directly, view it on GitHub https://github.com/ngx-formly/ngx-formly/issues/3680#issuecomment-1541266004, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADB4XNNOF76UMD3AK2JMES3XFMBALANCNFSM6AAAAAAXZ3Q354 . You are receiving this because you are subscribed to this thread.Message ID: @.***>

thanecoder commented 1 year ago

Issue happened when upgraded from version 6.1.3 to 6.1.5

aitboudad commented 1 year ago

Hi @aitboudad , pls suggest if any fix or workaround for this, as the forms are not changing from disabled to enabled state and vice versa.

use v6.1.4, I'll try to provide a fix late Today.

aitboudad commented 1 year ago

This issue has been fixed and released as part of v6.1.6 release.

Please let us know, in case you are still encountering a similar issue/problem. Thank you!