root / trunk / web / dojo / dijit / form / ValidationTextBox.js @ 13
History | View | Annotate | Download (7.26 KB)
1 | 9 | andrej.cim | /*
|
---|---|---|---|
2 | Copyright (c) 2004-2010, The Dojo Foundation All Rights Reserved.
|
||
3 | Available via Academic Free License >= 2.1 OR the modified BSD license.
|
||
4 | see: http://dojotoolkit.org/license for details
|
||
5 | */
|
||
6 | |||
7 | |||
8 | if(!dojo._hasResource["dijit.form.ValidationTextBox"]){ |
||
9 | dojo._hasResource["dijit.form.ValidationTextBox"]=true; |
||
10 | dojo.provide("dijit.form.ValidationTextBox");
|
||
11 | dojo.require("dojo.i18n");
|
||
12 | dojo.require("dijit.form.TextBox");
|
||
13 | dojo.require("dijit.Tooltip");
|
||
14 | dojo.requireLocalization("dijit.form","validate",null,"ROOT,ar,ca,cs,da,de,el,es,fi,fr,he,hu,it,ja,ko,nb,nl,pl,pt,pt-pt,ro,ru,sk,sl,sv,th,tr,zh,zh-tw"); |
||
15 | dojo.declare("dijit.form.ValidationTextBox",dijit.form.TextBox,{templateString:dojo.cache("dijit.form","templates/ValidationTextBox.html","<div class=\"dijit dijitReset dijitInlineTable dijitLeft\"\n\tid=\"widget_${id}\" waiRole=\"presentation\"\n\t><div class='dijitReset dijitValidationContainer'\n\t\t><input class=\"dijitReset dijitInputField dijitValidationIcon dijitValidationInner\" value=\"Χ \" type=\"text\" tabIndex=\"-1\" readOnly waiRole=\"presentation\"\n\t/></div\n\t><div class=\"dijitReset dijitInputField dijitInputContainer\"\n\t\t><input class=\"dijitReset dijitInputInner\" dojoAttachPoint='textbox,focusNode' autocomplete=\"off\"\n\t\t\t${!nameAttrSetting} type='${type}'\n\t/></div\n></div>\n"),baseClass:"dijitTextBox dijitValidationTextBox",required:false,promptMessage:"",invalidMessage:"$_unset_$",missingMessage:"$_unset_$",constraints:{},regExp:".*",regExpGen:function(_1){ |
||
16 | return this.regExp; |
||
17 | },state:"",tooltipPosition:[],_setValueAttr:function(){ |
||
18 | this.inherited(arguments); |
||
19 | this.validate(this._focused); |
||
20 | },validator:function(_2,_3){ |
||
21 | return (new RegExp("^(?:"+this.regExpGen(_3)+")"+(this.required?"":"?")+"$")).test(_2)&&(!this.required||!this._isEmpty(_2))&&(this._isEmpty(_2)||this.parse(_2,_3)!==undefined); |
||
22 | },_isValidSubset:function(){ |
||
23 | return this.textbox.value.search(this._partialre)==0; |
||
24 | },isValid:function(_4){ |
||
25 | return this.validator(this.textbox.value,this.constraints); |
||
26 | },_isEmpty:function(_5){ |
||
27 | return /^\s*$/.test(_5); |
||
28 | },getErrorMessage:function(_6){ |
||
29 | return (this.required&&this._isEmpty(this.textbox.value))?this.missingMessage:this.invalidMessage; |
||
30 | },getPromptMessage:function(_7){ |
||
31 | return this.promptMessage; |
||
32 | },_maskValidSubsetError:true,validate:function(_8){ |
||
33 | var _9=""; |
||
34 | var _a=this.disabled||this.isValid(_8); |
||
35 | if(_a){
|
||
36 | this._maskValidSubsetError=true; |
||
37 | } |
||
38 | var _b=this._isEmpty(this.textbox.value); |
||
39 | var _c=!_a&&!_b&&_8&&this._isValidSubset(); |
||
40 | this.state=((_a||((!this._hasBeenBlurred||_8)&&_b)||_c)&&this._maskValidSubsetError)?"":"Error"; |
||
41 | if(this.state=="Error"){ |
||
42 | this._maskValidSubsetError=_8;
|
||
43 | } |
||
44 | this._setStateClass();
|
||
45 | dijit.setWaiState(this.focusNode,"invalid",_a?"false":"true"); |
||
46 | if(_8){
|
||
47 | if(this.state=="Error"){ |
||
48 | _9=this.getErrorMessage(true); |
||
49 | }else{
|
||
50 | _9=this.getPromptMessage(true); |
||
51 | } |
||
52 | this._maskValidSubsetError=true; |
||
53 | } |
||
54 | this.displayMessage(_9);
|
||
55 | return _a;
|
||
56 | },_message:"",displayMessage:function(_d){ |
||
57 | if(this._message==_d){ |
||
58 | return;
|
||
59 | } |
||
60 | this._message=_d;
|
||
61 | dijit.hideTooltip(this.domNode);
|
||
62 | if(_d){
|
||
63 | dijit.showTooltip(_d,this.domNode,this.tooltipPosition,!this.isLeftToRight()); |
||
64 | } |
||
65 | },_refreshState:function(){ |
||
66 | this.validate(this._focused); |
||
67 | this.inherited(arguments); |
||
68 | },constructor:function(){ |
||
69 | this.constraints={};
|
||
70 | },_setConstraintsAttr:function(_e){ |
||
71 | if(!_e.locale&&this.lang){ |
||
72 | _e.locale=this.lang;
|
||
73 | } |
||
74 | this.constraints=_e;
|
||
75 | this._computePartialRE();
|
||
76 | },_computePartialRE:function(){ |
||
77 | var p=this.regExpGen(this.constraints); |
||
78 | this.regExp=p;
|
||
79 | var _f=""; |
||
80 | if(p!=".*"){ |
||
81 | this.regExp.replace(/\\.|\[\]|\[.*?[^\\]{1}\]|\{.*?\}|\(\?[=:!]|./g,function(re){ |
||
82 | switch(re.charAt(0)){ |
||
83 | case "{": |
||
84 | case "+": |
||
85 | case "?": |
||
86 | case "*": |
||
87 | case "^": |
||
88 | case "$": |
||
89 | case "|": |
||
90 | case "(": |
||
91 | _f+=re; |
||
92 | break;
|
||
93 | case ")": |
||
94 | _f+="|$)";
|
||
95 | break;
|
||
96 | default:
|
||
97 | _f+="(?:"+re+"|$)"; |
||
98 | break;
|
||
99 | } |
||
100 | }); |
||
101 | } |
||
102 | try{
|
||
103 | "".search(_f);
|
||
104 | } |
||
105 | catch(e){
|
||
106 | _f=this.regExp;
|
||
107 | console.warn("RegExp error in "+this.declaredClass+": "+this.regExp); |
||
108 | } |
||
109 | this._partialre="^(?:"+_f+")$"; |
||
110 | },postMixInProperties:function(){ |
||
111 | this.inherited(arguments); |
||
112 | this.messages=dojo.i18n.getLocalization("dijit.form","validate",this.lang); |
||
113 | if(this.invalidMessage=="$_unset_$"){ |
||
114 | this.invalidMessage=this.messages.invalidMessage; |
||
115 | } |
||
116 | if(!this.invalidMessage){ |
||
117 | this.invalidMessage=this.promptMessage; |
||
118 | } |
||
119 | if(this.missingMessage=="$_unset_$"){ |
||
120 | this.missingMessage=this.messages.missingMessage; |
||
121 | } |
||
122 | if(!this.missingMessage){ |
||
123 | this.missingMessage=this.invalidMessage; |
||
124 | } |
||
125 | this._setConstraintsAttr(this.constraints); |
||
126 | },_setDisabledAttr:function(_10){ |
||
127 | this.inherited(arguments); |
||
128 | this._refreshState();
|
||
129 | },_setRequiredAttr:function(_11){ |
||
130 | this.required=_11;
|
||
131 | dijit.setWaiState(this.focusNode,"required",_11); |
||
132 | this._refreshState();
|
||
133 | },reset:function(){ |
||
134 | this._maskValidSubsetError=true; |
||
135 | this.inherited(arguments); |
||
136 | },_onBlur:function(){ |
||
137 | this.displayMessage(""); |
||
138 | this.inherited(arguments); |
||
139 | }}); |
||
140 | dojo.declare("dijit.form.MappedTextBox",dijit.form.ValidationTextBox,{postMixInProperties:function(){ |
||
141 | this.inherited(arguments); |
||
142 | this.nameAttrSetting=""; |
||
143 | },serialize:function(val,_12){ |
||
144 | return val.toString?val.toString():""; |
||
145 | },toString:function(){ |
||
146 | var val=this.filter(this.get("value")); |
||
147 | return val!=null?(typeof val=="string"?val:this.serialize(val,this.constraints)):""; |
||
148 | },validate:function(){ |
||
149 | this.valueNode.value=this.toString(); |
||
150 | return this.inherited(arguments); |
||
151 | },buildRendering:function(){ |
||
152 | this.inherited(arguments); |
||
153 | this.valueNode=dojo.place("<input type='hidden'"+(this.name?" name='"+this.name+"'":"")+">",this.textbox,"after"); |
||
154 | },reset:function(){ |
||
155 | this.valueNode.value=""; |
||
156 | this.inherited(arguments); |
||
157 | }}); |
||
158 | dojo.declare("dijit.form.RangeBoundTextBox",dijit.form.MappedTextBox,{rangeMessage:"",rangeCheck:function(_13,_14){ |
||
159 | return ("min" in _14?(this.compare(_13,_14.min)>=0):true)&&("max" in _14?(this.compare(_13,_14.max)<=0):true); |
||
160 | },isInRange:function(_15){ |
||
161 | return this.rangeCheck(this.get("value"),this.constraints); |
||
162 | },_isDefinitelyOutOfRange:function(){ |
||
163 | var val=this.get("value"); |
||
164 | var _16=false; |
||
165 | var _17=false; |
||
166 | if("min" in this.constraints){ |
||
167 | var min=this.constraints.min; |
||
168 | min=this.compare(val,((typeof min=="number")&&min>=0&&val!=0)?0:min); |
||
169 | _16=(typeof min=="number")&&min<0; |
||
170 | } |
||
171 | if("max" in this.constraints){ |
||
172 | var max=this.constraints.max; |
||
173 | max=this.compare(val,((typeof max!="number")||max>0)?max:0); |
||
174 | _17=(typeof max=="number")&&max>0; |
||
175 | } |
||
176 | return _16||_17;
|
||
177 | },_isValidSubset:function(){ |
||
178 | return this.inherited(arguments)&&!this._isDefinitelyOutOfRange(); |
||
179 | },isValid:function(_18){ |
||
180 | return this.inherited(arguments)&&((this._isEmpty(this.textbox.value)&&!this.required)||this.isInRange(_18)); |
||
181 | },getErrorMessage:function(_19){ |
||
182 | var v=this.get("value"); |
||
183 | if(v!==null&&v!==""&&v!==undefined&&(typeof v!="number"||!isNaN(v))&&!this.isInRange(_19)){ |
||
184 | return this.rangeMessage; |
||
185 | } |
||
186 | return this.inherited(arguments); |
||
187 | },postMixInProperties:function(){ |
||
188 | this.inherited(arguments); |
||
189 | if(!this.rangeMessage){ |
||
190 | this.messages=dojo.i18n.getLocalization("dijit.form","validate",this.lang); |
||
191 | this.rangeMessage=this.messages.rangeMessage; |
||
192 | } |
||
193 | },_setConstraintsAttr:function(_1a){ |
||
194 | this.inherited(arguments); |
||
195 | if(this.focusNode){ |
||
196 | if(this.constraints.min!==undefined){ |
||
197 | dijit.setWaiState(this.focusNode,"valuemin",this.constraints.min); |
||
198 | }else{
|
||
199 | dijit.removeWaiState(this.focusNode,"valuemin"); |
||
200 | } |
||
201 | if(this.constraints.max!==undefined){ |
||
202 | dijit.setWaiState(this.focusNode,"valuemax",this.constraints.max); |
||
203 | }else{
|
||
204 | dijit.removeWaiState(this.focusNode,"valuemax"); |
||
205 | } |
||
206 | } |
||
207 | },_setValueAttr:function(_1b,_1c){ |
||
208 | dijit.setWaiState(this.focusNode,"valuenow",_1b); |
||
209 | this.inherited(arguments); |
||
210 | }}); |
||
211 | } |