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

dependentKeys as a function? #677

Open
erikmellum opened this issue May 14, 2020 · 3 comments
Open

dependentKeys as a function? #677

erikmellum opened this issue May 14, 2020 · 3 comments

Comments

@erikmellum
Copy link

My dependentKeys are dynamic and can't be known at run time. Is it possible to use a function that returns an array of dependentKeys?

@erikmellum
Copy link
Author

Alternatively, is there a way to force revalidation on validators?

@erikmellum
Copy link
Author

erikmellum commented May 14, 2020

I was able to force this validation to retrigger with some hacking, but surely there is a better way:

// Force a revalidation by changing customFields to a new ember object with the same content
        items.forEach(i => {
          i.set('customFields', EmberObject.create(i.get('customFields')));
        });

I also tried strategies to no avail such as: items[0].validations._validators.customFields[0].validate(items[0].get('customFields'), { dependentKeys: ["model.type", "model.customFields"] }, items[0]) (ugly but this was just for testing purposes).

@erikmellum
Copy link
Author

Last thing I'll add in case it helps, the actual inline validator:

customFields: validator('inline', {
    dependentKeys: ['model.type', 'model.customFields'],
    validate(value, options, model) {
      const fields = getTypeFields(model);
      const errors = [];
      fields.forEach(field => {
        if (field.get('isRequired') && isBlank(model.get(`customFields.${field.get('name')}`))) {
          errors.push(field.get('name'));
        }
      });
      model.set('errorMessages', errors);
      if (Object.values(errors).length) {
        return Object.values(errors).join(', ');
      }
      return true;
    },
  }),

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