]> git.proxmox.com Git - extjs.git/blame - extjs/packages/core/src/data/ErrorCollection.js
bump version to 7.0.0-4
[extjs.git] / extjs / packages / core / src / data / ErrorCollection.js
CommitLineData
947f0963
TL
1/**
2 * Wraps a collection of validation error responses and provides convenient functions for
3 * accessing and errors for specific fields.
4 *
5 * Usually this class does not need to be instantiated directly - instances are instead
6 * created automatically when {@link Ext.data.Model#validate validate} on a model instance:
7 *
8 * // Validate some existing model instance - in this case it returned 2 failures
9 * // messages
10 *
11 * var errors = myModel.validate();
12 * errors.isValid(); //false
13 *
14 * errors.length; //2
15 * errors.getByField('name'); // [{field: 'name', message: 'must be present'}]
16 * errors.getByField('title'); // [{field: 'title', message: 'is too short'}]
17 */
18Ext.define('Ext.data.ErrorCollection', {
19 extend: 'Ext.util.MixedCollection', // not Ext.util.Collection due to API differences
20
21 alternateClassName: 'Ext.data.Errors',
22
23 requires: [
24 'Ext.data.Error'
25 ],
26
27 init: function(record) {
28 var me = this,
29 fields = record.fields,
30 data = record.data,
31 before, field, i, len, msg, val, name;
32
33 for (i = 0, len = fields.length; i < len; ++i) {
34 field = fields[i];
35 name = field.name;
36 val = data[name];
37
38 if (field.validate && !field.validate.$nullFn) {
39 before = me.length;
40 msg = field.validate(val, null, me, record);
41
42 if (before === me.length && msg !== true) {
43 me.add(name, msg);
44 }
45 }
46 }
47
48 return me;
49 },
50
51 add: function(key, value) {
52 var me = this,
53 defaultMessage = Ext.data.field.Field.defaultInvalidMessage,
54 obj = key, // for single argument form
55 current;
56
57 if (Ext.isString(key)) {
58 obj = new Ext.data.Error({
59 field: key,
60 message: value || defaultMessage
61 });
62 }
63 else {
64 if (!(obj.isError)) {
65 obj = new Ext.data.Error({
66 field: obj.field || obj.name,
67 message: obj.error || obj.message || obj.msg || defaultMessage
68 });
69 }
70
71 key = obj.field;
72 }
73
74 current = me.get(key);
75
76 if (current) {
77 if (Ext.isArray(current)) {
78 current.push(obj);
79
80 return current;
81 }
82
83 me.removeAtKey(key);
84 obj = [ current, obj ];
85 obj.field = key;
86
87 // Because the value we want in the collection is an array, we need to wrap it
88 // another layer of array or the base add method will add each element.
89 obj = [ obj ];
90 }
91
92 return me.callParent([ obj ]);
93 },
94
95 getKey: function(item) {
96 return item.field;
97 },
98
99 /**
100 * Returns true if there are no errors in the collection
101 * @return {Boolean}
102 */
103 isValid: function() {
104 return this.length === 0;
105 },
106
107 /**
108 * Returns all of the errors for the given field
109 * @param {String} fieldName The field to get errors for
110 * @return {Object[]} All errors for the given field
111 */
112 getByField: function(fieldName) {
113 var values = this.get(fieldName);
114
115 if (values && !Ext.isArray(values)) {
116 values = [values];
117 }
118
119 return values || [];
120 }
121});