Skip to content

Instantly share code, notes, and snippets.

@offirgolan
Last active February 4, 2016 19:29
Show Gist options
  • Save offirgolan/4518559f3903054dcf00 to your computer and use it in GitHub Desktop.
Save offirgolan/4518559f3903054dcf00 to your computer and use it in GitHub Desktop.
conditional validations
import Ember from 'ember';
import DS from 'ember-data';
import { buildValidations, validator } from 'ember-cp-validations';
/**
* Only enable validations on proName and proTeam if join_as_pro is set to true
*/
const Validations = buidlValidations({
name: [ /* name validations */ ],
password: [ /* password validations */ ],
proName: {
dependentKeys: ['join_as_pro'],
disabled() {
return !this.get('model.join_as_pro');
},
validations: [ /* proName validations */ ]
},
proTeam: {
dependentKeys: ['join_as_pro'],
disabled() {
return !this.get('model.join_as_pro');
},
validations: [ /* proTeam validations */ ]
}
})
export default DS.Model.extend(Validations, {
name: DS.attr('string'),
password: DS.attr('string'),
proName: DS.attr('string'),
proTeam: DS.attr('string'),
join_as_pro: false
})
@ianformanek
Copy link

It looks like the buildValidations function does not expect the object instead of the validation directly, I get this:

Error: Assertion Failed: Cannot call get with 'dependentKeys' on an undefined object.
    at new Error (native)
    at Error.EmberError (http://localhost:4000/assets/vendor.js:25715:21)
    at assert (http://localhost:4000/assets/vendor.js:15663:13)
    at Object.assert (http://localhost:4000/assets/vendor.js:25485:34)
    at get (http://localhost:4000/assets/vendor.js:29602:22)
    at getWithDefault (http://localhost:4000/assets/vendor.js:29728:17)
    at http://localhost:4000/assets/vendor.js:70096:43
    at Array.forEach (native)
    at getCPDependentKeysFor (http://localhost:4000/assets/vendor.js:70075:17)
    at createCPValidationFor (http://localhost:4000/assets/vendor.js:70027:25)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment