]>
Commit | Line | Data |
---|---|---|
6527f429 DM |
1 | /**\r |
2 | * @abstract\r | |
3 | * A superclass for a validator that checks if a value is within a certain range.\r | |
4 | */\r | |
5 | Ext.define('Ext.data.validator.Bound', {\r | |
6 | extend: 'Ext.data.validator.Validator',\r | |
7 | alias: 'data.validator.bound',\r | |
8 | \r | |
9 | type: 'bound',\r | |
10 | \r | |
11 | config: {\r | |
12 | /**\r | |
13 | * @cfg {Number} min\r | |
14 | * The minimum length value.\r | |
15 | */\r | |
16 | min: undefined,\r | |
17 | \r | |
18 | /**\r | |
19 | * @cfg {Number} max\r | |
20 | * The maximum length value.\r | |
21 | */\r | |
22 | max: undefined,\r | |
23 | \r | |
24 | /**\r | |
25 | * @cfg {String} emptyMessage\r | |
26 | * The error message to return when the value is empty.\r | |
27 | */\r | |
28 | emptyMessage: 'Must be present',\r | |
29 | \r | |
30 | /**\r | |
31 | * @cfg {String} minOnlyMessage\r | |
32 | * The error message to return when the value is less than the minimum\r | |
33 | * and only a minimum is specified.\r | |
34 | */\r | |
35 | minOnlyMessage: null,\r | |
36 | \r | |
37 | /**\r | |
38 | * @cfg {String} maxOnlyMessage\r | |
39 | * The error message to return when the value is more than the maximum\r | |
40 | * and only a maximum is specified.\r | |
41 | */\r | |
42 | maxOnlyMessage: null,\r | |
43 | \r | |
44 | /**\r | |
45 | * @cfg {String} bothMessage\r | |
46 | * The error message to return when the value is not in the specified range\r | |
47 | * and both the minimum and maximum are specified.\r | |
48 | */\r | |
49 | bothOnlyMessage: null\r | |
50 | },\r | |
51 | \r | |
52 | constructor: function() {\r | |
53 | var me = this;\r | |
54 | \r | |
55 | me.preventConfigure = true;\r | |
56 | me.callParent(arguments);\r | |
57 | delete me.preventConfigure;\r | |
58 | me.configure();\r | |
59 | },\r | |
60 | \r | |
61 | setConfig: function() {\r | |
62 | var me = this;\r | |
63 | \r | |
64 | me.preventConfigure = true; \r | |
65 | me.callParent(arguments);\r | |
66 | delete me.preventConfigure;\r | |
67 | me.configure();\r | |
68 | },\r | |
69 | \r | |
70 | configure: function() {\r | |
71 | var me = this,\r | |
72 | hasMin, hasMax,\r | |
73 | min, max;\r | |
74 | \r | |
75 | if (me.preventConfigure) {\r | |
76 | return;\r | |
77 | }\r | |
78 | \r | |
79 | min = me.getMin();\r | |
80 | max = me.getMax();\r | |
81 | \r | |
82 | hasMin = me.hasMin = min !== undefined;\r | |
83 | hasMax = me.hasMax = max !== undefined;\r | |
84 | \r | |
85 | if (hasMin && hasMax) {\r | |
86 | me._bothMsg = Ext.String.format(me.getBothMessage(), min, max); \r | |
87 | } else if (hasMin) {\r | |
88 | me._minMsg = Ext.String.format(me.getMinOnlyMessage(), min);\r | |
89 | } else if (hasMax) {\r | |
90 | me._maxMsg = Ext.String.format(me.getMaxOnlyMessage(), max);\r | |
91 | } \r | |
92 | },\r | |
93 | \r | |
94 | updateMin: function() {\r | |
95 | this.configure(); \r | |
96 | },\r | |
97 | \r | |
98 | updateMax: function() {\r | |
99 | this.configure(); \r | |
100 | },\r | |
101 | \r | |
102 | updateMinOnlyMessage: function(v) {\r | |
103 | this.configure(); \r | |
104 | },\r | |
105 | \r | |
106 | updateMaxOnlyMessage: function() {\r | |
107 | this.configure(); \r | |
108 | },\r | |
109 | \r | |
110 | updateBothMessage: function() {\r | |
111 | this.configure(); \r | |
112 | },\r | |
113 | \r | |
114 | validate: function(value) {\r | |
115 | var me = this,\r | |
116 | hasMin = me.hasMin,\r | |
117 | hasMax = me.hasMax,\r | |
118 | min = me.getMin(),\r | |
119 | max = me.getMax(),\r | |
120 | msg = this.validateValue(value),\r | |
121 | len;\r | |
122 | \r | |
123 | if (msg !== true) {\r | |
124 | return msg;\r | |
125 | }\r | |
126 | \r | |
127 | value = me.getValue(value);\r | |
128 | if (hasMin && hasMax) {\r | |
129 | if (value < min || value > max) {\r | |
130 | msg = me._bothMsg;\r | |
131 | }\r | |
132 | } else if (hasMin) {\r | |
133 | if (value < min) {\r | |
134 | msg = me._minMsg;\r | |
135 | }\r | |
136 | } else if (hasMax) {\r | |
137 | if (value > max) {\r | |
138 | msg = me._maxMsg;\r | |
139 | } \r | |
140 | }\r | |
141 | \r | |
142 | return msg;\r | |
143 | },\r | |
144 | \r | |
145 | validateValue: function(value) {\r | |
146 | if (value === undefined || value === null) {\r | |
147 | return this.getEmptyMessage();\r | |
148 | }\r | |
149 | return true;\r | |
150 | },\r | |
151 | \r | |
152 | getValue: Ext.identityFn\r | |
153 | });\r |