Skip to content

Instantly share code, notes, and snippets.

@Munter
Created December 15, 2010 18:53
Show Gist options
  • Save Munter/742414 to your computer and use it in GitHub Desktop.
Save Munter/742414 to your computer and use it in GitHub Desktop.
/*
Adds an 'optional' property to data type configuration, which allows undefined or null as values.
*/
Ext.iterate(Ext.data.Types, function(key, value, obj) {
if (value.convert) {
var fn = value.convert;
obj[key] = {
convert: function (v) {
return (this.optional && (v === undefined || v === null)) ? null : fn(v);
},
sortType: value.sortType,
type: value.type
};
}
});
/*
Adds an 'optional' property to data validation configuration, which allows null as value.
*/
Ext.iterate(Ext.data.validations, function(key, value, obj) {
if (Ext.isFunction(value)) {
var fn = value;
obj[key] = function (config, value) {
return (value === null && config.optional) || fn(config, value);
}
}
});
Ext.regModel('Email', {
belongsTo: 'Contact',
fields: [
{ name: 'value', type: 'string' },
{ name: 'type', type: 'string', optional: true, defaultValue: null },
{ name: 'primary', type: 'boolean'}
],
validations: [
{ type: 'presence', field: 'value' },
{ type: 'email', field: 'value' },
{ type: 'inclusion', field: 'type', list: ['home', 'work', 'other'], optional: true }
]
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment