PDA

View Full Version : Filter methods in Ext.data.Errors should return an instance of Ext.data.Errors



esatterwhite
27 Jul 2011, 6:51 AM
Sencha Touch version tested:

1.1


Platform tested against:

all


Description:

Ext.data.Errors Extends MixedCollection, but does not override the filterBy method which creates and returns a new Mixed collection. One can not filter an Errors object and then use the Error methods isValid(), getByField() as Mixed Collections do not have them.


Test Case:



Ext.regModel('Dummy',{

fields:[
{name:'email', type:'string', }
,{name:'dummy', type:'string',}
]

,validations:[
{
field:'email'
,type:'format'
,matcher:/^[\w\.-]+@[\w\.-]+\.\w+$/i
}

,{field:'password', type:'presence'}
]
});
var instance = Ext.ModelMgr.create({}, 'Dummy')
instance.set('email','mail@mail.com');
var errors = instance.validate();

errors = errors.filterBy( function( obj, key ){
return obj.field !=='dummy'
});

var valid = errors.isValid();




Steps to reproduce the problem:

create a model with some validations
create an instance of the model
set fields with invalid data
validate the model
filter a field out of the Errors Object
run isValid() on the resulting MixedCollection Object


The result that was expected:

The Errors object should return a boolean indicating if it is valid or not


The result that occurs instead:

A javascript error is thrown indicating that the isValid method does not exist


Possible fix:
this fixed the problem in my situation


Ext.override( Ext.data.Errors,{

filterBy: function(fn, scope){
var newErr = new Ext.data.Errors(),
keys = this.keys,
items = this.items,
length = items.length,
i;

newErr.getKey = this.getKey;

for (i = 0; i < length; i++) {
if (fn.call(scope||this, items[i], keys[i])) {
newErr.add(keys[i], items[i]);
}
}

return newErr;
}
})