return false instead of throwing exception
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / BaseValidator.cs
1 /**
2  * Namespace: System.Web.UI.WebControls
3  * Class:     BaseValidator
4  *
5  * Authors:  Gaurav Vaish, Gonzalo Paniagua Javier
6  * Maintainer: gvaish@iitk.ac.in
7  * Contact: <my_scripts2001@yahoo.com>, <gvaish@iitk.ac.in>, <gonzalo@ximian.com>
8  * Implementation: yes
9  * Status:  80%
10  *
11  * (C) Gaurav Vaish (2001)
12  * (c) 2002 Ximian, Inc. (http://www.ximian.com)
13  */
14
15 using System;
16 using System.ComponentModel;
17 using System.Web;
18 using System.Web.UI;
19 using System.Drawing;
20
21 namespace System.Web.UI.WebControls
22 {
23         [DefaultProperty("ErrorMessage")]
24         //TODO: [Designer("??")]
25         public abstract class BaseValidator: Label, IValidator
26         {
27                 private bool isValid;
28                 private bool isPreRenderCalled;
29                 private bool isPropertiesChecked;
30                 private bool propertiesValid;
31                 private bool renderUplevel;
32
33                 protected BaseValidator() : base()
34                 {
35                         isValid = true;
36                         ForeColor = Color.Red;
37                         propertiesValid = true;
38                         isPropertiesChecked = false;
39                         renderUplevel = false;
40                 }
41
42                 public string ControlToValidate
43                 {
44                         get
45                         {
46                                 object o = ViewState["ControlToValidate"];
47                                 if(o != null)
48                                 {
49                                         return (string)o;
50                                 }
51                                 return String.Empty;
52                         }
53                         set
54                         {
55                                 ViewState["ControlToValidate"] = value;
56                         }
57                 }
58
59                 public ValidatorDisplay Display
60                 {
61                         get
62                         {
63                                 object o = ViewState["Display"];
64                                 if(o != null)
65                                 {
66                                         return (ValidatorDisplay)o;
67                                 }
68                                 return ValidatorDisplay.Static;
69                         }
70                         set
71                         {
72                                 if(!Enum.IsDefined(typeof(ValidatorDisplay), value))
73                                 {
74                                         throw new ArgumentException();
75                                 }
76                                 ViewState["ValidatorDisplay"] = value;
77                         }
78                 }
79
80                 public bool EnableClientScript
81                 {
82                         get
83                         {
84                                 object o = ViewState["EnableClientScript"];
85                                 if(o != null)
86                                 {
87                                         return (bool)o;
88                                 }
89                                 return true;
90                         }
91                         set
92                         {
93                                 ViewState["EnableClientScript"] = value;
94                         }
95                 }
96
97                 public override bool Enabled
98                 {
99                         get
100                         {
101                                 return base.Enabled;
102                         }
103                         set
104                         {
105                                 if (value == false)
106                                         isValid = true;
107                                 base.Enabled = value;
108                         }
109                 }
110
111                 public string ErrorMessage
112                 {
113                         get
114                         {
115                                 object o = ViewState["ErrorMessage"];
116                                 if(o != null)
117                                 {
118                                         return (string)o;
119                                 }
120                                 return String.Empty;
121                         }
122                         set
123                         {
124                                 ViewState["ErrorMessage"] = value;
125                         }
126                 }
127
128                 public override Color ForeColor
129                 {
130                         get
131                         {
132                                 return base.ForeColor;
133                         }
134                         set
135                         {
136                                 base.ForeColor = value;
137                         }
138                 }
139
140                 public bool IsValid
141                 {
142                         get { return isValid; }
143                         set { isValid = value; }
144                 }
145
146                 public static PropertyDescriptor GetValidationProperty(object component)
147                 {
148                         System.ComponentModel.AttributeCollection coll = TypeDescriptor.GetAttributes (component);
149                         Type type = typeof (ValidationPropertyAttribute);
150                         ValidationPropertyAttribute attrib = (ValidationPropertyAttribute) coll [type];
151                         if (attrib != null && attrib.Name != null)
152                                 return (TypeDescriptor.GetProperties (component)) [attrib.Name];
153                         return null;
154                 }
155
156                 public void Validate()
157                 {
158                         if(!Visible || (Visible && !Enabled))
159                         {
160                                 IsValid = true;
161                         }
162                         Control ctrl = Parent;
163                         while(ctrl != null)
164                         {
165                                 if(!ctrl.Visible)
166                                 {
167                                         IsValid = true;
168                                         return;
169                                 }
170                                 ctrl = ctrl.Parent;
171                         }
172                         isPropertiesChecked = false;
173                         if(!PropertiesValid)
174                         {
175                                 IsValid = true;
176                                 return;
177                         }
178                         IsValid = EvaluateIsValid();
179                 }
180
181                 protected bool PropertiesValid
182                 {
183                         get
184                         {
185                                 if(!isPropertiesChecked)
186                                 {
187                                         propertiesValid = ControlPropertiesValid();
188                                         isPropertiesChecked = true;
189                                 }
190                                 return propertiesValid;
191                         }
192                 }
193
194                 protected bool RenderUplevel
195                 {
196                         get
197                         {
198                                 return renderUplevel;
199                         }
200                 }
201
202                 protected override void AddAttributesToRender(HtmlTextWriter writer)
203                 {
204                         bool enabled = base.Enabled;
205                         if (enabled)
206                                 Enabled = true;
207
208                         base.AddAttributesToRender(writer);
209                         if(RenderUplevel)
210                         {
211                                 if(ID == null)
212                                 {
213                                         writer.AddAttribute("id", ClientID);
214                                 }
215                                 if(ControlToValidate.Length > 0)
216                                 {
217                                         writer.AddAttribute("controltovalidate", GetControlRenderID(ControlToValidate));
218                                 }
219                                 if(ErrorMessage.Length > 0)
220                                 {
221                                         writer.AddAttribute("errormessage", ErrorMessage, true);
222                                 }
223                                 if(Display == ValidatorDisplay.Static)
224                                 {
225                                         writer.AddAttribute("display", Enum.Format(typeof(ValidatorDisplay), Display, "G").Replace('_','-'));
226                                         //writer.AddAttribute("display", PropertyConverter.EnumToString(typeof(ValidatorDisplay), Display));
227                                 }
228                                 if(!IsValid)
229                                 {
230                                         writer.AddAttribute("isvalid", "False");
231                                 }
232
233                                 ControlStyle.AddAttributesToRender (writer, this);
234                                 if(!enabled)
235                                 {
236                                         writer.AddAttribute("enabled", "False");
237                                 }
238                         }
239
240                         if(enabled)
241                         {
242                                 Enabled = false;
243                         }
244                 }
245
246                 protected void CheckControlValidationProperty(string name, string propertyName)
247                 {
248                         Control ctrl = NamingContainer.FindControl(name);
249                         if(ctrl == null)
250                         {
251                                 throw new HttpException(HttpRuntime.FormatResourceString("Validator_control_not_found",
252                                                  name, propertyName/*, ID*/));
253                         }
254                         PropertyDescriptor pd = GetValidationProperty(ctrl);
255                         if(pd == null)
256                         {
257                                 throw new HttpException(HttpRuntime.FormatResourceString("Validator_bad_control_type",
258                                                  name, propertyName/*, ID*/));
259                         }
260                 }
261
262                 protected virtual bool ControlPropertiesValid()
263                 {
264                         if(ControlToValidate.Length == 0)
265                         {
266                                 throw new HttpException(HttpRuntime.FormatResourceString("Validator_control_blank", ID));
267                         }
268                         CheckControlValidationProperty(ControlToValidate, "ControlToValidate");
269                         return true;
270                 }
271
272                 [MonoTODO]
273                 protected virtual bool DetermineRenderUplevel()
274                 {
275                         Page page = Page;
276                         if(page == null || page.Request == null)
277                         {
278                                 return false;
279                         }
280
281                         if(EnableClientScript)
282                         {
283                                 // By now, return false
284                                 return false;
285                                 //throw new NotImplementedException();
286                                 ////TODO: I need to get the (Browser->Dom_version_major >= 4 &&
287                                 ////                         Brower->Ecma_script_version >= 1.2)
288                         }
289                         return false;
290                 }
291
292                 protected string GetControlRenderID(string name)
293                 {
294                         Control ctrl = FindControl(name);
295                         if(ctrl != null)
296                         {
297                                 return ctrl.ClientID;
298                         }
299                         return String.Empty;
300                 }
301
302                 protected string GetControlValidationValue(string name)
303                 {
304                         Control ctrl = NamingContainer.FindControl(name);
305                         if(ctrl != null)
306                         {
307                                 PropertyDescriptor pd = GetValidationProperty(ctrl);
308                                 if(pd != null)
309                                 {
310                                         object item = pd.GetValue(ctrl);
311                                         if(item is ListItem)
312                                         {
313                                                 return ((ListItem)item).Value;
314                                         }
315                                         return item.ToString();
316                                 }
317                         }
318                         return null;
319                 }
320
321                 protected override void OnInit(EventArgs e)
322                 {
323                         base.OnInit(e);
324                         Page.Validators.Add(this);
325                 }
326
327                 protected override void OnPreRender(EventArgs e)
328                 {
329                         base.OnPreRender(e);
330                         isPreRenderCalled   = true;
331                         isPropertiesChecked = false;
332                         renderUplevel       = DetermineRenderUplevel();
333                         if(renderUplevel)
334                         {
335                                 RegisterValidatorCommonScript();
336                         }
337                 }
338
339                 protected override void OnUnload(EventArgs e)
340                 {
341                         if(Page != null)
342                         {
343                                 Page.Validators.Remove(this);
344                         }
345                         base.OnUnload(e);
346                 }
347
348                 [MonoTODO("Damn_This_Is_Really_Frustrating___by_Gaurav")]
349                 protected void RegisterValidatorCommonScript()
350                 {
351                         if(Page.IsClientScriptBlockRegistered("ValidatorIncludeScript"))
352                                 return;
353                         
354                         throw new NotImplementedException();
355                 }
356
357                 [MonoTODO("I_have_to_know_javascript_for_this_I_know_it_but_for_ALL_browsers_NO")]
358                 protected virtual void RegisterValidatorDeclaration()
359                 {
360                         throw new NotImplementedException();
361                         //TODO: Since I have to access document.<ClientID> and register
362                         // as page validator. Now this is Browser dependent :((
363
364                         // This does not render anything on Mozilla, and does on IE
365                 }
366
367                 [MonoTODO("Render_ing_always_left")]
368                 protected override void Render (HtmlTextWriter writer)
369                 {
370                         bool valid;
371
372                         if (!Enabled)
373                                 return;
374
375                         if (isPreRenderCalled) {
376                                 valid = IsValid;
377                         } else {
378                                 isPropertiesChecked = true;
379                                 propertiesValid     = true;
380                                 renderUplevel       = false;
381                                 valid               = true;
382                         }
383
384                         if (PropertiesValid) {
385                                 if (Page != null)
386                                         Page.VerifyRenderingInServerForm (this);
387
388                                 ValidatorDisplay dis = Display;
389                                 if (RenderUplevel) {
390                                         //FIXME: as of now, don't do client-side validation
391                                         throw new NotImplementedException();
392                                 }
393                                 
394                                 if (!valid) {
395                                         RenderBeginTag (writer);
396                                         if (Text.Trim ().Length > 0 || HasControls ()) {
397                                                 RenderContents (writer);
398                                                 RenderEndTag (writer);
399                                         } else {
400                                                 writer.Write (ErrorMessage);
401                                         }
402                                         RenderEndTag (writer);
403                                         return;
404                                 }
405                         } else {
406                                 writer.Write ("&nbsp;");
407                         }
408                 }
409
410                 protected abstract bool EvaluateIsValid();
411         }
412 }