-
-
Save nucleartide/9e5694d6f493a30c88d42123fc6d9af3 to your computer and use it in GitHub Desktop.
ember-changeset-validations demo
This file contains 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 AdultValidations from '../validations/adult'; | |
import ChildValidations from '../validations/child'; | |
import { reservedEmails } from '../validators/uniqueness'; | |
import { schema } from '../models/user'; | |
const { get } = Ember; | |
const { keys } = Object; | |
export default Ember.Controller.extend({ | |
AdultValidations, | |
ChildValidations, | |
reservedEmails, | |
actions: { | |
save(changeset) { | |
let snapshot = changeset.snapshot(); | |
return changeset | |
.cast(keys(schema)) | |
.validate() | |
.then(() => { | |
if (get(changeset, 'isValid')) { | |
return changeset.execute(); | |
} | |
}).catch(() => { | |
changeset.restore(snapshot); | |
}); | |
}, | |
reset(changeset) { | |
return changeset.rollback(); | |
}, | |
validateProperty(changeset, property) { | |
return changeset.validate(property); | |
} | |
} | |
}); |
This file contains 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'; | |
import Ember from 'ember'; | |
const { computed: { alias } } = Ember; | |
export const schema = { | |
line: attr('string'), | |
district: attr('string'), | |
country: attr('string'), | |
state: alias('district') | |
}; | |
export default Model.extend(schema); |
This file contains 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 = { | |
firstName: attr('string'), | |
lastName: attr('string'), | |
email: attr('string'), | |
age: attr('number'), | |
job: attr('string'), | |
address: belongsTo('address') | |
}; | |
export default Model.extend(schema); |
This file contains 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() { | |
let user = this.get('store').createRecord('user', { | |
firstName: 'Jim', | |
lastName: 'Bob', | |
age: 10 | |
}); | |
let a = this.get('store').createRecord('address', { | |
line: '' | |
}); | |
user.set('address', a) | |
return user; | |
} | |
}); |
This file contains 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": "2.12.0", | |
"ember-template-compiler": "2.12.0", | |
"ember-testing": "2.12.0", | |
"skeleton-css": "https://cdnjs.cloudflare.com/ajax/libs/skeleton/2.0.4/skeleton.min.css" | |
}, | |
"addons": { | |
"ember-data": "2.12.1", | |
"ember-changeset-validations": "1.2.9", | |
"ember-one-way-controls": "0.8.3" | |
} | |
} |
This file contains 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 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 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 { | |
firstName: [ | |
validatePresence(true), | |
validateLength({ min: 2 }) | |
], | |
lastName: [ | |
validatePresence(true), | |
validateLength({ min: 2 }) | |
], | |
email: [ | |
validateUniqueness(), | |
validateFormat({ type: 'email' }) | |
], | |
job: validatePresence(true) | |
} |
This file contains 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 = ['foo@bar.com', 'admin@example.com']; | |
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