Upgrade to ExtJS 4.0.1 - Released 05/18/2011
[extjs.git] / src / data / Errors.js
1 /**
2  * @author Ed Spencer
3  * @class Ext.data.Errors
4  * @extends Ext.util.MixedCollection
5  * 
6  * <p>Wraps a collection of validation error responses and provides convenient functions for
7  * accessing and errors for specific fields.</p>
8  * 
9  * <p>Usually this class does not need to be instantiated directly - instances are instead created
10  * automatically when {@link Ext.data.Model#validate validate} on a model instance:</p>
11  * 
12 <pre><code>
13 //validate some existing model instance - in this case it returned 2 failures messages
14 var errors = myModel.validate();
15
16 errors.isValid(); //false
17
18 errors.length; //2
19 errors.getByField('name');  // [{field: 'name',  message: 'must be present'}]
20 errors.getByField('title'); // [{field: 'title', message: 'is too short'}]
21 </code></pre>
22  */
23 Ext.define('Ext.data.Errors', {
24     extend: 'Ext.util.MixedCollection',
25     
26     /**
27      * Returns true if there are no errors in the collection
28      * @return {Boolean} 
29      */
30     isValid: function() {
31         return this.length === 0;
32     },
33     
34     /**
35      * Returns all of the errors for the given field
36      * @param {String} fieldName The field to get errors for
37      * @return {Array} All errors for the given field
38      */
39     getByField: function(fieldName) {
40         var errors = [],
41             error, field, i;
42             
43         for (i = 0; i < this.length; i++) {
44             error = this.items[i];
45             
46             if (error.field == fieldName) {
47                 errors.push(error);
48             }
49         }
50         
51         return errors;
52     }
53 });