ember-changeset-validations icon indicating copy to clipboard operation
ember-changeset-validations copied to clipboard

Dependent field validation capability

Open ctwoolsey opened this issue 3 years ago • 0 comments

It would be really nice when writing a custom validator to be able to refer to other fields in the validation object. Currently for this level of validation, I have to write code when the field changes to do validation in my component and then use 'changeset.addError' which isn't working as expected. issue 651 ember-changeset

for example:

export default {
  fieldA: [validatePresence(true)],
  fieldB: [validatePresence(true)],
  complexField: validateCustom()]
}

complexField might validate on the value of 'fieldA' and/or 'fieldB'. For example:

export default function validateCustom({ min, max } = {}) {
  return (key, newValue, oldValue, changes, content) => {
    if (fieldA === 'A'  && newValue === 'Yes it is A') {
      return true;
    }
    if (fieldB=== 'B'  && newValue === 'Yes it is B') {
      return true;
    }
    return false;
  }
}

ctwoolsey avatar Jun 24 '22 13:06 ctwoolsey