Ember CP validations disable


#1

So I am using ember cp validations and I want to enable/disable a rule based off of a property that is set via a component

// Model/Customer

const Validations = buildValidations({
  firstName: validator('presence', {
    message: "Please enter your first name",
    presence: true
  }),
  lastName: validator('presence', {
    message: "Please enter your last name",
    presence: true
  }),
  email: [
    validator('presence', {
      message: "Please enter an email address",
      presence: true
    }),
    validator('format', {
      message: "Please enter a valid email address",
      type: 'email'
    })
  ],
  cellPhone: [
    validator('presence', {
      disabled: readOnly('shouldCheckCellPhone'),
      message: "Please enter a phone number",
      presence: true
    }),
    validator('format', {
      disabled: readOnly('shouldCheckCellPhone'),
      message: "Please enter a valid phone number",
      type: 'phone'
    })
  ]
});

export default DS.Model.extend(HasManyQuery.ModelMixin, Validations, {
  email: DS.attr('string'),
  firstName: DS.attr('string'),
  cellPhone: DS.attr('string'),
  lastName: DS.attr('string'),
  shouldCheckCellPhone: false,
});

Here is my component

didReceiveAttrs() {
    this._super(...arguments);

    this.set('model.shouldCheckCellPhone', !this
      .get('requireMobilePhoneCarrier'));
  },

Thoughts?


#2

I might be missing some details, but I’d use good ol’ DDAU (Data Down Actions Up) and send an action “up” from the component that is handled in the controller. That action should probably set the shouldCheckCellPhone in the model eventually and then all is good.


#3

I got it to work. That is basically what I did @balint