2 * Namespace: System.Web.UI.WebControls
\r
3 * Class: BaseValidator
\r
5 * Authors: Gaurav Vaish, Gonzalo Paniagua Javier
\r
6 * Maintainer: gvaish@iitk.ac.in
\r
7 * Contact: <my_scripts2001@yahoo.com>, <gvaish@iitk.ac.in>, <gonzalo@ximian.com>
\r
8 * Implementation: yes
\r
11 * (C) Gaurav Vaish (2001)
\r
12 * (c) 2002 Ximian, Inc. (http://www.ximian.com)
\r
16 using System.ComponentModel;
\r
18 using System.Web.UI;
\r
19 using System.Drawing;
\r
21 namespace System.Web.UI.WebControls
\r
23 [DefaultProperty("ErrorMessage")]
\r
24 //TODO: [Designer("??")]
\r
25 public abstract class BaseValidator: Label, IValidator
\r
27 private bool isValid;
\r
28 private bool isPreRenderCalled;
\r
29 private bool isPropertiesChecked;
\r
30 private bool propertiesValid;
\r
31 private bool renderUplevel;
\r
33 protected BaseValidator() : base()
\r
36 ForeColor = Color.Red;
\r
37 propertiesValid = true;
\r
38 isPropertiesChecked = false;
\r
39 renderUplevel = false;
\r
42 public string ControlToValidate
\r
46 object o = ViewState["ControlToValidate"];
\r
51 return String.Empty;
\r
55 ViewState["ControlToValidate"] = value;
\r
59 public ValidatorDisplay Display
\r
63 object o = ViewState["Display"];
\r
66 return (ValidatorDisplay)o;
\r
68 return ValidatorDisplay.Static;
\r
72 if(!Enum.IsDefined(typeof(ValidatorDisplay), value))
\r
74 throw new ArgumentException();
\r
76 ViewState["ValidatorDisplay"] = value;
\r
80 public bool EnableClientScript
\r
84 object o = ViewState["EnableClientScript"];
\r
93 ViewState["EnableClientScript"] = value;
\r
97 public override bool Enabled
\r
101 return base.Enabled;
\r
105 if (value == false)
\r
107 base.Enabled = value;
\r
111 public string ErrorMessage
\r
115 object o = ViewState["ErrorMessage"];
\r
120 return String.Empty;
\r
124 ViewState["ErrorMessage"] = value;
\r
128 public override Color ForeColor
\r
132 return base.ForeColor;
\r
136 base.ForeColor = value;
\r
140 public bool IsValid
\r
142 get { return isValid; }
\r
143 set { isValid = value; }
\r
146 public static PropertyDescriptor GetValidationProperty(object component)
\r
148 System.ComponentModel.AttributeCollection coll = TypeDescriptor.GetAttributes (component);
\r
149 Type type = typeof (ValidationPropertyAttribute);
\r
150 ValidationPropertyAttribute attrib = (ValidationPropertyAttribute) coll [type];
\r
151 if (attrib != null && attrib.Name != null)
\r
152 return (TypeDescriptor.GetProperties (component)) [attrib.Name];
\r
156 public void Validate()
\r
158 if(!Visible || (Visible && !Enabled))
\r
162 Control ctrl = Parent;
\r
163 while(ctrl != null)
\r
170 ctrl = ctrl.Parent;
\r
172 isPropertiesChecked = false;
\r
173 if(!PropertiesValid)
\r
178 IsValid = EvaluateIsValid();
\r
181 protected bool PropertiesValid
\r
185 if(!isPropertiesChecked)
\r
187 propertiesValid = ControlPropertiesValid();
\r
188 isPropertiesChecked = true;
\r
190 return propertiesValid;
\r
194 protected bool RenderUplevel
\r
198 return renderUplevel;
\r
202 protected override void AddAttributesToRender(HtmlTextWriter writer)
\r
204 bool enabled = base.Enabled;
\r
208 base.AddAttributesToRender(writer);
\r
213 writer.AddAttribute("id", ClientID);
\r
215 if(ControlToValidate.Length > 0)
\r
217 writer.AddAttribute("controltovalidate", GetControlRenderID(ControlToValidate));
\r
219 if(ErrorMessage.Length > 0)
\r
221 writer.AddAttribute("errormessage", ErrorMessage, true);
\r
223 if(Display == ValidatorDisplay.Static)
\r
225 writer.AddAttribute("display", Enum.Format(typeof(ValidatorDisplay), Display, "G").Replace('_','-'));
\r
226 //writer.AddAttribute("display", PropertyConverter.EnumToString(typeof(ValidatorDisplay), Display));
\r
230 writer.AddAttribute("isvalid", "False");
\r
233 ControlStyle.AddAttributesToRender (writer, this);
\r
236 writer.AddAttribute("enabled", "False");
\r
246 protected void CheckControlValidationProperty(string name, string propertyName)
\r
248 Control ctrl = NamingContainer.FindControl(name);
\r
251 throw new HttpException(HttpRuntime.FormatResourceString("Validator_control_not_found",
\r
252 name, propertyName/*, ID*/));
\r
254 PropertyDescriptor pd = GetValidationProperty(ctrl);
\r
257 throw new HttpException(HttpRuntime.FormatResourceString("Validator_bad_control_type",
\r
258 name, propertyName/*, ID*/));
\r
262 protected virtual bool ControlPropertiesValid()
\r
264 if(ControlToValidate.Length == 0)
\r
266 throw new HttpException(HttpRuntime.FormatResourceString("Validator_control_blank", ID));
\r
268 CheckControlValidationProperty(ControlToValidate, "ControlToValidate");
\r
273 protected virtual bool DetermineRenderUplevel()
\r
276 if(page == null || page.Request == null)
\r
281 if(EnableClientScript)
\r
283 // By now, return false
\r
285 ////throw new NotImplementedException();
\r
286 ////TODO: I need to get the (Browser->Dom_version_major >= 4 &&
\r
287 //// Brower->Ecma_script_version >= 1.2)
\r
292 protected string GetControlRenderID(string name)
\r
294 Control ctrl = FindControl(name);
\r
297 return ctrl.ClientID;
\r
299 return String.Empty;
\r
302 protected string GetControlValidationValue(string name)
\r
304 Control ctrl = NamingContainer.FindControl(name);
\r
307 PropertyDescriptor pd = GetValidationProperty(ctrl);
\r
310 object item = pd.GetValue(ctrl);
\r
311 if(item is ListItem)
\r
313 return ((ListItem)item).Value;
\r
315 return item.ToString();
\r
321 protected override void OnInit(EventArgs e)
\r
324 Page.Validators.Add(this);
\r
327 protected override void OnPreRender(EventArgs e)
\r
329 base.OnPreRender(e);
\r
330 isPreRenderCalled = true;
\r
331 isPropertiesChecked = false;
\r
332 renderUplevel = DetermineRenderUplevel();
\r
335 RegisterValidatorCommonScript();
\r
339 protected override void OnUnload(EventArgs e)
\r
343 Page.Validators.Remove(this);
\r
348 [MonoTODO("What_do_I_have_to_do")]
\r
349 protected void RegisterValidatorCommonScript()
\r
352 //throw new NotImplementedException();
\r
355 [MonoTODO("I_have_to_know_javascript_for_this_I_know_it_but_for_ALL_browsers_NO")]
\r
356 protected virtual void RegisterValidatorDeclaration()
\r
358 throw new NotImplementedException();
\r
359 //TODO: Since I have to access document.<ClientID> and register
\r
360 // as page validator. Now this is Browser dependent :((
\r
362 // This does not render anything on Mozilla, and does on IE
\r
365 [MonoTODO("Render_ing_always_left")]
\r
366 protected override void Render (HtmlTextWriter writer)
\r
373 if (isPreRenderCalled) {
\r
376 isPropertiesChecked = true;
\r
377 propertiesValid = true;
\r
378 renderUplevel = false;
\r
382 if (PropertiesValid) {
\r
384 Page.VerifyRenderingInServerForm (this);
\r
386 ValidatorDisplay dis = Display;
\r
387 if (RenderUplevel) {
\r
388 //FIXME: by now, don't do client-side validation
\r
392 RenderBeginTag (writer);
\r
393 if (Text.Trim ().Length > 0 || HasControls ()) {
\r
394 RenderContents (writer);
\r
395 RenderEndTag (writer);
\r
397 writer.Write (ErrorMessage);
\r
399 RenderEndTag (writer);
\r
403 writer.Write (" ");
\r
407 protected abstract bool EvaluateIsValid();
\r