Comments (9)
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?
from ember-changeset-validations.
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?
from ember-changeset-validations.
@celicoo I think the goal is to be able to programmatically enable/disable validations based on the value of other fields in the changeset.
from ember-changeset-validations.
+1
It would ve great if we could enable or disable each validation rule on the fly.
from ember-changeset-validations.
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.
from ember-changeset-validations.
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.
from ember-changeset-validations.
@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 :)
from ember-changeset-validations.
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()]
}
from ember-changeset-validations.
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;
}
]```
from ember-changeset-validations.
Related Issues (20)
- async validator is called multiple times with the same value HOT 2
- Date validator does not respect 'rawOutput' option HOT 2
- async validators can complete out of order HOT 4
- Documentation for rawOutput
- (Octane) skipValidate=true in helper does not work HOT 2
- (Octane) Documentation update suggestion: passing the validation instance HOT 1
- ember inspector silently polluting `changes`? HOT 1
- Button attribute not updating using classic components syntax HOT 1
- Access Service from validator class
- Freezed value in Input HOT 5
- Update to ember-validates v4 is breaking change HOT 2
- allowBlank doesn't work with validateFormat() HOT 1
- [Proposal] Use isEmpty for the date validator's allowBlank alike the other validators
- Use of `assign` has been deprecated
- [rfc]: Externalize schema validation HOT 4
- Strange behavior with an array of objects HOT 2
- Dependent field validation capability
- Docs pages are missing
- `description` option type missing for validators
- Provided description for validation not passed in rawOutput for buildMessage
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ember-changeset-validations.