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

Skipping a validator

Open jaredgalanis opened this issue 7 years ago • 9 comments

I have a validation file which holds a complete set of validators for a particular template. The template conditionally renders certain fields. Even when those fields are not rendered the validators appear to be triggering an error on the changeset (since I am using validatePresence and nothing is being entered, since it is not rendered). Is there a way to skip a validator?

jaredgalanis avatar Jan 14 '17 23:01 jaredgalanis

I am now setting the property to 0 if the condition is fulfilled which causes the above input not to be rendered. This is probably acceptable for this use case, but I wonder if there might circumstances in the future where the property should be left as null or undefined and not set to 0 (i.e. there might be another place where this property is tested for undefined/null), and where skipping the validator would be a preferable option.

jaredgalanis avatar Jan 15 '17 14:01 jaredgalanis

I've been playing around with a pattern like this for conditional validations over the past couple days.

It seems to work really nicely with the exception of needing to explicitly revalidate dependent fields whenever a field involved in a condition changes — I noticed the existing confirmation validator has the same issue where it doesn't revalidate if the value of its on field changes.

@poteto Do you have any thoughts on what first-class support for dependent validations might look like?

dfreeman avatar Jan 18 '17 23:01 dfreeman

+1 It would ve great if we could enable or disable each validation rule on the fly.

fpalluel avatar Jan 24 '17 12:01 fpalluel

@celicoo I think the goal is to be able to programmatically enable/disable validations based on the value of other fields in the changeset.

dfreeman avatar Jan 30 '17 18:01 dfreeman

@dfreeman To be honest I have not, but I like what your conditional validator does. Let me experiment a little more. In the meantime, if you would like to make your conditional validator a plugin to e-c-v that would be great :)

poteto avatar Feb 21 '17 06:02 poteto

I encountered this use case as well and came up with the below. It's not too pretty but I think it's straightforward and versatile:

// Validate presence only if underlying model isNew
{
  quantity: [
    function(key, newValue, oldValue, changes, content) {
      if (content.get('isNew')) {
        return validatePresence(true)(...arguments);
      }
      return true;
    }
  ]
}

You could of course make it dependent on a value in changes. Given the philosophy behind validators being "just functions", I think it should work pretty well?

wongpeiyi avatar Jul 14 '17 08:07 wongpeiyi

Would be great if this was supported out of the box. I'm currently building a multi-step form, and would like to validate certain fields at a time.

mschinis avatar Jan 23 '19 20:01 mschinis

I encountered this use case as well and came up with the below. It's not too pretty but I think it's straightforward and versatile:

// Validate presence only if underlying model isNew
{
  quantity: [
    function(key, newValue, oldValue, changes, content) {
      if (content.get('isNew')) {
        return validatePresence(true)(...arguments);
      }
      return true;
    }
  ]
}

You could of course make it dependent on a value in changes. Given the philosophy behind validators being "just functions", I think it should work pretty well?

How can I do that using more than one validator? Something like this:

if (content.get('isNew')) {
  return [validatePresence(true)(...arguments), myCustomValidator()]
}

yandiro avatar Apr 08 '22 02:04 yandiro

quantity: [
    function(key, newValue, oldValue, changes, content) {
      if (content.get('isNew')) {
        return validatePresence(true)(...arguments);
      }
      return true;
    }
  ]

The answer to my own question would be :

quantity: [
    function(key, newValue, oldValue, changes, content) {
      if (content.get('condition1')) {
        return validatePresence(true)(...arguments);
      }
      return true;
    },
    function(key, newValue, oldValue, changes, content) {
      if (content.get('condition2')) {
        return validateSomethingElse(true)(...arguments);
      }
      return true;
    }
  ]```

yandiro avatar Apr 08 '22 06:04 yandiro