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

forms-utils: add the async validators up the chain #80

Open
gparlakov opened this issue Jun 19, 2022 · 1 comment
Open

forms-utils: add the async validators up the chain #80

gparlakov opened this issue Jun 19, 2022 · 1 comment

Comments

@gparlakov
Copy link
Owner

When using the forAllControlsIn and addValidatorsTo the async validators are not supported.

@gparlakov
Copy link
Owner Author

Perhaps :

    addValidatorsTo(parentControl: AbstractControl | null | undefined) {
      if (parentControl != null) {
        parentControl.validator = Validators.compose([
          parentControl.validator,
          () => {
            // could overwrite some errors - but we only need it to communicate to the "parent" form that
            // these controls here are valid or not
            const errors = controls.reduce((e, next) => ({ ...e, ...next.errors }), {});

            return controls.some(c => c.errors != null) ? errors : null;
          }
        ]);

        parentControl.asyncValidator = Validators.composeAsync([
          parentControl.asyncValidator,
          async (c) => {

            return Promise.all(controls
                .filter(c => typeof c?.asyncValidator ==='function')
                .map(c => typeof c?.asyncValidator ==='function' ? isObservable(c.asyncValidator(c) : Promise.resolve()
              )
              .then((results: (object | null)[]) => results.filter(o => o != null))


          }
        ])
      }

      return composer;
    }

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

1 participant