-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tests green like the "Rey de COPAS"!
- Loading branch information
1 parent
d04cabb
commit c5dac81
Showing
5 changed files
with
152 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,79 @@ | ||
import Ember from 'ember'; | ||
|
||
export default Ember.Mixin.create({ | ||
validationErrors: {}, | ||
isValidNow: true, | ||
validate: function() { | ||
var store = this.get('store'), | ||
errors = null; | ||
|
||
// Clean all the current errors | ||
this.get('errors').clear(); | ||
this.set('validationErrors',{}); | ||
this.set('isValidNow',true); | ||
errors = this.get('validationErrors'); | ||
|
||
// Check for presence: | ||
this._validatePresence(); | ||
// Check for valid emails: | ||
this._validateEmail(); | ||
// Check relations: | ||
this._validateRelations(); | ||
if (!this.get('isValidNow')) { | ||
// It may be invalid because of its relations | ||
if(Object.keys(errors).length !== 0){ | ||
this.transitionTo('updated.uncommitted'); | ||
store.recordWasInvalid(this, errors); | ||
} | ||
return false; | ||
}else{ | ||
return true; | ||
} | ||
}, | ||
_validatePresence: function() { | ||
var _this = this, | ||
errors = this.get('validationErrors'), | ||
validations = this.get('validations'); | ||
if(validations && validations.hasOwnProperty('presence')) { | ||
validations.presence.forEach(function(property) { | ||
if (Ember.isBlank(_this.get(property))){ | ||
if (!Ember.isArray(errors[property])) {errors[property] = [];} | ||
_this.set('isValidNow',false); | ||
errors[property].push(['This field is required']); | ||
} | ||
}); | ||
} | ||
}, | ||
_validateEmail: function() { | ||
var _this = this, | ||
errors = this.get('validationErrors'), | ||
validations = this.get('validations'); | ||
if(validations && validations.hasOwnProperty('email')) { | ||
validations.email.forEach(function(property) { | ||
if (_this.get(property) && _this.get(property).match(/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i) === null){ | ||
if (!Ember.isArray(errors[property])) {errors[property] = [];} | ||
_this.set('isValidNow',false); | ||
errors[property].push(['Enter a valid email address']); | ||
} | ||
}); | ||
} | ||
}, | ||
_validateRelations: function() { | ||
var _this = this, | ||
validations = this.get('validations'); | ||
if(validations && validations.hasOwnProperty("relations")) { | ||
if(validations.relations.hasOwnProperty("hasMany")) { | ||
validations.relations.hasMany.forEach(function(relation) { | ||
if(_this.get(relation)){ | ||
_this.get(relation).forEach(function(objRelation) { | ||
if(!objRelation.validate()){ | ||
_this.set('isValidNow',false); | ||
} | ||
}); | ||
} | ||
}); | ||
} | ||
} | ||
} | ||
|
||
}); |
Empty file.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import DS from 'ember-data'; | ||
import Validator from '../mixins/validator'; | ||
|
||
export default DS.Model.extend(Validator,{ | ||
name: DS.attr('string'), | ||
email: DS.attr('string'), | ||
|
||
otherFakes: DS.hasMany('other-model'), | ||
|
||
validations: { | ||
presence: ['name','email'], | ||
email: ['email'], | ||
relations: { | ||
hasMany:['otherFakes'] | ||
} | ||
} | ||
|
||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import DS from 'ember-data'; | ||
import Validator from '../mixins/validator'; | ||
|
||
export default DS.Model.extend(Validator,{ | ||
name: DS.attr('string'), | ||
email: DS.attr('string'), | ||
|
||
validations: { | ||
presence: ['name','email'], | ||
email: ['email'] | ||
} | ||
|
||
}); |
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