parent
e1abc5e4cb
commit
f41f4caccd
17 changed files with 221 additions and 77 deletions
30
app/mixins/validation.js
Normal file
30
app/mixins/validation.js
Normal file
|
@ -0,0 +1,30 @@
|
|||
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();
|
||||
},
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue