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