ccdb-web/app/mixins/validation.js
2017-10-07 17:37:24 -07:00

30 lines
749 B
JavaScript

import Ember from 'ember';
const { Mixin, get } = Ember;
const { keys } = Object;
export default Mixin.create({
validationSave(changeset, schema, postSave) {
return changeset
.cast(keys(schema))
.validate()
.then(() => {
if (changeset.get('isValid')) {
return changeset.save().then(postSave);
}
})
.catch((error) => {
/* eslint-disable no-console */
console.log(error);
/* eslint-enable no-console */
get(this, 'model.errors').forEach(({ attribute, message }) => {
changeset.pushErrors(attribute, message);
});
});
},
validationCancel(changeset, postCancel) {
changeset.rollback();
return postCancel();
},
});