-
-
Save muziejus/73dda1e77a1b3a878a903f7065e655b4 to your computer and use it in GitHub Desktop.
ember-changeset-validations demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
import { | |
validatePresence, | |
validateLength | |
} from 'ember-changeset-validations/validators'; | |
const { get } = Ember; | |
export default Ember.Controller.extend({ | |
props: ["name.firstName", "name.lastName", "job", "age"], | |
get FixedValidations() { | |
const validations = { | |
"name.firstName": [ | |
validatePresence(true), | |
validateLength({ min: 2 }) | |
], | |
"name.lastName": [ | |
validatePresence(true), | |
validateLength({ min: 2 }) | |
], | |
job: validatePresence(true), | |
age: validatePresence(true) | |
}; | |
console.log("Fixed Validations", validations); | |
return validations; | |
}, | |
get ProgrammaticValidations() { | |
const validations = {}; | |
this.props.forEach(property => { | |
if (/Name/.test(property)) { | |
validations[property] = [ | |
validatePresence(true), | |
validateLength({ min: 2 }) | |
]; | |
} else { | |
validations[property] = validatePresence(true); | |
} | |
}) | |
console.log("Programmatic Validations", validations); | |
return validations; | |
}, | |
actions: { | |
save(changeset) { | |
let snapshot = changeset.snapshot(); | |
return changeset | |
.validate() | |
.then(() => { | |
if (get(changeset, 'isValid')) { | |
console.log("executing to model."); | |
return changeset.execute(); | |
} | |
}).catch(() => { | |
changeset.restore(snapshot); | |
}); | |
}, | |
reset(changeset) { | |
return changeset.rollback(); | |
}, | |
validateProperty(changeset, property) { | |
return changeset.validate(property); | |
} | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Model from 'ember-data/model'; | |
import attr from 'ember-data/attr'; | |
import { belongsTo, hasMany } from 'ember-data/relationships'; | |
export const schema = { | |
name: attr(), | |
age: attr('number'), | |
job: attr('string') | |
}; | |
export default Model.extend(schema); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
export default Ember.Route.extend({ | |
model() { | |
return this.get('store').createRecord('user', { | |
name: { | |
firstName: 'Jim', | |
lastName: 'Bob' | |
}, | |
age: 18 | |
}); | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"version": "0.12.1", | |
"EmberENV": { | |
"FEATURES": {} | |
}, | |
"options": { | |
"use_pods": false, | |
"enable-testing": false | |
}, | |
"dependencies": { | |
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js", | |
"ember": "3.4.3", | |
"ember-template-compiler": "3.4.3", | |
"ember-testing": "3.4.3", | |
"skeleton-css": "https://cdnjs.cloudflare.com/ajax/libs/skeleton/2.0.4/skeleton.min.css" | |
}, | |
"addons": { | |
"ember-data": "2.12.1", | |
"ember-changeset": "2.1.2", | |
"ember-changeset-validations": "2.1.0", | |
"ember-one-way-controls": "0.8.3" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
import { validateNumber } from 'ember-changeset-validations/validators'; | |
import UserValidations from './user'; | |
const { assign } = Ember; | |
export default assign({}, UserValidations, { | |
age: validateNumber({ gte: 18 }) | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
import { validateNumber } from 'ember-changeset-validations/validators'; | |
import UserValidations from './user'; | |
const { assign } = Ember; | |
export default assign({}, UserValidations, { | |
age: validateNumber({ lt: 18 }) | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { | |
validatePresence, | |
validateLength, | |
validateFormat | |
} from 'ember-changeset-validations/validators'; | |
import validateUniqueness from '../validators/uniqueness'; | |
export default { | |
"name.firstName": [ | |
validatePresence(true), | |
validateLength({ min: 2 }) | |
], | |
"name.lastName": [ | |
validatePresence(true), | |
validateLength({ min: 2 }) | |
], | |
job: validatePresence(true) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
const { RSVP: { resolve } } = Ember; | |
export const reservedEmails = ['[email protected]', '[email protected]']; | |
export default function validateUniqueness() { | |
return (key, newValue, oldValue, changes) => { | |
let isAvailable = reservedEmails.indexOf(newValue) === -1; | |
return resolve(isAvailable || 'is taken'); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment