ember-changeset-validations
ember-changeset-validations copied to clipboard
Skipping a validator
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?
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.
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?
+1 It would ve great if we could enable or disable each validation rule on the fly.
@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 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 :)
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?
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.
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()]
}
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;
}
]```