Upgrade to ExtJS 4.0.7 - Released 10/19/2011
[extjs.git] / src / data / Errors.js
1 /*
2
3 This file is part of Ext JS 4
4
5 Copyright (c) 2011 Sencha Inc
6
7 Contact:  http://www.sencha.com/contact
8
9 GNU General Public License Usage
10 This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file.  Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
11
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
13
14 */
15 /**
16  * @author Ed Spencer
17  * @class Ext.data.Errors
18  * @extends Ext.util.MixedCollection
19  *
20  * <p>Wraps a collection of validation error responses and provides convenient functions for
21  * accessing and errors for specific fields.</p>
22  *
23  * <p>Usually this class does not need to be instantiated directly - instances are instead created
24  * automatically when {@link Ext.data.Model#validate validate} on a model instance:</p>
25  *
26 <pre><code>
27 //validate some existing model instance - in this case it returned 2 failures messages
28 var errors = myModel.validate();
29
30 errors.isValid(); //false
31
32 errors.length; //2
33 errors.getByField('name');  // [{field: 'name',  message: 'must be present'}]
34 errors.getByField('title'); // [{field: 'title', message: 'is too short'}]
35 </code></pre>
36  */
37 Ext.define('Ext.data.Errors', {
38     extend: 'Ext.util.MixedCollection',
39
40     /**
41      * Returns true if there are no errors in the collection
42      * @return {Boolean}
43      */
44     isValid: function() {
45         return this.length === 0;
46     },
47
48     /**
49      * Returns all of the errors for the given field
50      * @param {String} fieldName The field to get errors for
51      * @return {Object[]} All errors for the given field
52      */
53     getByField: function(fieldName) {
54         var errors = [],
55             error, field, i;
56
57         for (i = 0; i < this.length; i++) {
58             error = this.items[i];
59
60             if (error.field == fieldName) {
61                 errors.push(error);
62             }
63         }
64
65         return errors;
66     }
67 });
68