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

validation depending on status of other field

Open kchirayus opened this issue 9 years ago • 3 comments

Environment

  • Ember Version: 2.8.1
  • Ember CP Validations Version: 3.0.1

Steps to Reproduce

I'm trying to validate a dropdown value only if a checkbox is selected. How would I do this:

Model: export default DS.Model.extend(Validations, { age_restriction: DS.attr('number', {defaultValue: 0}), isAgeRestricted: Ember.computed.gt('age_restriction', 0), });

isAgeRestricted will return a boolean value. I need to validate the age_restriction is greater than zero only if isAgeRestricted is true.

kchirayus avatar Oct 13 '16 19:10 kchirayus

In your validations you can use the disabled option.

disabled: computed.not('model.isAgeRestricted')

offirgolan avatar Oct 13 '16 19:10 offirgolan

This is what I tried:

const Validations = build({
  age_restriction: validator('number', {
    disabled: Ember.computed.not('model.isAgeRestricted'),
    gt: 0
  })
});

Now it's invalid when I select the checkbox and the value is higher than zero

kchirayus avatar Oct 13 '16 20:10 kchirayus

@kchirayus can we close the issue?

fsmanuel avatar Sep 10 '22 09:09 fsmanuel