**** Merged from MCS ****
[mono-project.git] / mcs / class / System.Web.Mobile / System.Web.UI.MobileControls / BaseValidator.cs
blob79c7c368ba3c91960c147c031ca9af9b01bc725b
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining
4 // a copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to
8 // permit persons to whom the Software is furnished to do so, subject to
9 // the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be
12 // included in all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 /**
23 * Project : Mono
24 * Namespace : System.Web.UI.MobileControls
25 * Class : BaseValidator
26 * Author : Gaurav Vaish
28 * Copyright : 2003 with Gaurav Vaish, and with
29 * Ximian Inc
32 using System.ComponentModel;
33 using System.Web.UI;
34 using System.Web.Mobile;
35 using System.Web.UI.WebControls;
37 namespace System.Web.UI.MobileControls
39 public abstract class BaseValidator : TextControl, IValidator
41 private System.Web.UI.WebControls.BaseValidator webBaseValidator;
42 private bool isValid = true;
44 protected BaseValidator()
46 StyleReference = "error";
47 webBaseValidator = CreateWebValidator();
48 if(webBaseValidator == null)
49 this.webBaseValidator = new DefaultWebValidator();
50 Controls.Add(webBaseValidator);
51 webBaseValidator.Display = ValidatorDisplay.Dynamic;
54 private class DefaultWebValidator
55 : System.Web.UI.WebControls.BaseValidator
57 protected override bool EvaluateIsValid()
59 return false;
63 protected virtual System.Web.UI.WebControls.BaseValidator CreateWebValidator()
65 return null;
68 public string ErrorMessage
70 get
72 return webBaseValidator.ErrorMessage;
74 set
76 webBaseValidator.ErrorMessage = value;
80 public bool IsValid
82 get
84 return isValid;
86 set
88 isValid = value;
92 public void Validate()
94 if(Visible)
96 Control parent = Parent;
97 bool visible = true;
98 while(parent != null)
100 if(!parent.Visible)
102 visible = false;
103 break;
105 parent = parent.Parent;
107 if(visible)
108 EvaluateIsValid();
109 } else
111 IsValid = true;
115 protected abstract bool EvaluateIsValid();
117 public string ControlToValidate
121 return webBaseValidator.ControlToValidate;
125 webBaseValidator.ControlToValidate = value;
129 public ValidatorDisplay Display
133 return webBaseValidator.Display;
137 webBaseValidator.Display = value;
141 public override string StyleReference
145 return base.StyleReference;
149 base.StyleReference = value;
153 public override int VisibleWeight
157 return 0;
161 protected void CheckControlValidationProperty(string name,
162 string propertyName)
164 Control ctrl = NamingContainer.FindControl(name);
165 if(ctrl == null)
167 // FIXME
168 throw new ArgumentException("BaseValidator_ControlNotFound");
170 PropertyDescriptor pd = System.Web.UI.WebControls.BaseValidator.GetValidationProperty(ctrl);
171 if(pd == null)
173 // FIXME
174 throw new ArgumentException("BaseValidator_BadControlType");
178 protected virtual bool ControlPropertiesValid()
180 string ctrl = ControlToValidate;
181 if(ctrl.Length == 0)
183 // FIXME
184 throw new ArgumentException("BaseValidator_ControlToValidateBlank");
186 CheckControlValidationProperty(ctrl, "ControlToValidate");
187 return true;
190 internal bool EvaluateIsValidInternal()
194 webBaseValidator.Validate();
195 } catch(Exception)
197 string thisID = ID;
198 ID = webBaseValidator.ID;
199 webBaseValidator.ID = thisID;
202 webBaseValidator.Validate();
203 } finally
205 thisID = ID;
206 ID = webBaseValidator.ID;
207 webBaseValidator.ID = thisID;
210 return webBaseValidator.IsValid;
213 protected override void OnInit(EventArgs e)
215 Page.Validators.Add(this);
216 base.OnInit(e);
219 protected override void OnPreRender(EventArgs e)
221 if(MobilePage.ActiveForm == Form)
222 ControlPropertiesValid();
223 base.OnPreRender(e);