Created
December 16, 2010 14:59
-
-
Save Munter/743486 to your computer and use it in GitHub Desktop.
Deep validation of Sencha Touch models
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
Ext.override(Ext.data.Model, { | |
/** | |
* Validates the current data against all of its configured {@link #validations} and returns an | |
* {@link Ext.data.Errors Errors} object | |
* @param {Boolean} deep Boolean to indicate whether to validate relations as well | |
* @return {Ext.data.Errors} The errors object | |
*/ | |
validate: function(deep) { | |
var errors = new Ext.data.Errors(), | |
validations = this.validations, | |
validators = Ext.data.validations, | |
length, validation, field, association, valid, type, i; | |
if (validations) { | |
length = validations.length; | |
for (i = 0; i < length; i++) { | |
validation = validations[i]; | |
field = validation.field || validation.name; | |
type = validation.type; | |
associationName = validation.association; | |
if (type && field) { | |
valid = validators[type](validation, this.get(field)); | |
if (!valid) { | |
errors.add({ | |
field : field, | |
message: validation.message || validators[type + 'Message'], | |
scope: this | |
}); | |
} | |
} else if (deep && associationName && this.associations.map[associationName]) { | |
this[associationName]().each(function (record) { | |
var errs = record.validate(deep); | |
if (!errs.isValid()) { | |
errors.addAll(errs.items); | |
} | |
}); | |
} | |
} | |
} | |
return errors; | |
} | |
}); |
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
Ext.regModel('Contact', { | |
fields: [ | |
// Portable Contacts Singular fields | |
{ name: 'id', type: 'string' }, | |
{ name: 'displayName', type: 'string' }, | |
{ name: 'nickname', type: 'string' }, | |
{ name: 'published', type: 'date', dateFormat: 'c' }, | |
{ name: 'updated', type: 'date', dateFormat: 'c' }, | |
{ name: 'birthday', type: 'date', dateFormat: 'c' }, | |
{ name: 'anniversary', type: 'date', dateFormat: 'c' }, | |
{ name: 'gender', type: 'string' }, | |
{ name: 'note', type: 'string' }, | |
{ name: 'preferredUsername', type: 'string' }, | |
{ name: 'utcOffset', type: 'date', dateFormat: 'P' }, | |
{ name: 'connected', type: 'boolean', defaultValue: false } | |
], | |
// Plural fields | |
hasMany: [ | |
{ model: 'Email', name: 'emails' }, | |
{ model: 'URL', name: 'urls' }, | |
{ model: 'Phone', name: 'phoneNumbers' }, | |
{ model: 'IM', name: 'ims' }, | |
{ model: 'Photo', name: 'photos' }, | |
{ model: 'Tag', name: 'tags' }, | |
{ model: 'Address', name: 'addresses' }, | |
{ model: 'Organization', name: 'organizations' } | |
], | |
validations: [ | |
{ association: 'emails' }, | |
{ association: 'urls' }, | |
{ association: 'phoneNumbers' }, | |
{ association: 'ims' }, | |
{ association: 'photos' }, | |
{ association: 'addresses' }, | |
{ association: 'organizations' } | |
] | |
}); |
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
Ext.regModel('Email', { | |
belongsTo: 'Contact', | |
fields: [ | |
{ name: 'value', type: 'string' }, | |
{ name: 'type', type: 'string', optional: true, defaultValue: null }, | |
{ name: 'primary', type: 'boolean', optional: true, defaultValue: null } | |
], | |
validations: [ | |
{ field: 'value', type: 'presence' }, | |
{ field: 'value', type: 'email' }, | |
{ field: 'type', type: 'inclusion', list: ['home', 'work', 'other'], optional: true }, | |
{ field: 'primary', type: 'pocoprimary' } | |
] | |
}); |
I am keeping an updated patch branch for ExtJS 4 on https://github.com/One-com/Ext-JS-4/tree/denormalized_4.0.2a
The probably outdated Sancha Touch patch branch is here: https://github.com/One-com/Sencha-Touch/tree/denormalized_1.1.0
Also I have started this forum thread on the subject: http://www.sencha.com/forum/showthread.php?127547-Sencha-Platform-denormalized-Data-patch
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I can't find anything relating to this in the official Sencha Touch documentation or source code, have you been in touch with them regarding them possibility of them implementing the same behaviour (or just including your function into their code base..)?