2 * Namespace: System.Web.UI.WebControls
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>
11 * (C) Gaurav Vaish (2001)
12 * (c) 2002 Ximian, Inc. (http://www.ximian.com)
16 using System.ComponentModel;
21 namespace System.Web.UI.WebControls
23 [DefaultProperty("ErrorMessage")]
24 //TODO: [Designer("??")]
25 public abstract class BaseValidator: Label, IValidator
28 private bool isPreRenderCalled;
29 private bool isPropertiesChecked;
30 private bool propertiesValid;
31 private bool renderUplevel;
33 protected BaseValidator() : base()
36 ForeColor = Color.Red;
37 propertiesValid = true;
38 isPropertiesChecked = false;
39 renderUplevel = false;
42 public string ControlToValidate
46 object o = ViewState["ControlToValidate"];
55 ViewState["ControlToValidate"] = value;
59 public ValidatorDisplay Display
63 object o = ViewState["Display"];
66 return (ValidatorDisplay)o;
68 return ValidatorDisplay.Static;
72 if(!Enum.IsDefined(typeof(ValidatorDisplay), value))
74 throw new ArgumentException();
76 ViewState["ValidatorDisplay"] = value;
80 public bool EnableClientScript
84 object o = ViewState["EnableClientScript"];
93 ViewState["EnableClientScript"] = value;
97 public override bool Enabled
107 base.Enabled = value;
111 public string ErrorMessage
115 object o = ViewState["ErrorMessage"];
124 ViewState["ErrorMessage"] = value;
128 public override Color ForeColor
132 return base.ForeColor;
136 base.ForeColor = value;
142 get { return isValid; }
143 set { isValid = value; }
146 public static PropertyDescriptor GetValidationProperty(object component)
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];
156 public void Validate()
158 if(!Visible || (Visible && !Enabled))
162 Control ctrl = Parent;
172 isPropertiesChecked = false;
178 IsValid = EvaluateIsValid();
181 protected bool PropertiesValid
185 if(!isPropertiesChecked)
187 propertiesValid = ControlPropertiesValid();
188 isPropertiesChecked = true;
190 return propertiesValid;
194 protected bool RenderUplevel
198 return renderUplevel;
202 protected override void AddAttributesToRender(HtmlTextWriter writer)
204 bool enabled = base.Enabled;
208 base.AddAttributesToRender(writer);
213 writer.AddAttribute("id", ClientID);
215 if(ControlToValidate.Length > 0)
217 writer.AddAttribute("controltovalidate", GetControlRenderID(ControlToValidate));
219 if(ErrorMessage.Length > 0)
221 writer.AddAttribute("errormessage", ErrorMessage, true);
223 if(Display == ValidatorDisplay.Static)
225 writer.AddAttribute("display", Enum.Format(typeof(ValidatorDisplay), Display, "G").Replace('_','-'));
226 //writer.AddAttribute("display", PropertyConverter.EnumToString(typeof(ValidatorDisplay), Display));
230 writer.AddAttribute("isvalid", "False");
233 ControlStyle.AddAttributesToRender (writer, this);
236 writer.AddAttribute("enabled", "False");
246 protected void CheckControlValidationProperty(string name, string propertyName)
248 Control ctrl = NamingContainer.FindControl(name);
251 throw new HttpException(HttpRuntime.FormatResourceString("Validator_control_not_found",
252 name, propertyName/*, ID*/));
254 PropertyDescriptor pd = GetValidationProperty(ctrl);
257 throw new HttpException(HttpRuntime.FormatResourceString("Validator_bad_control_type",
258 name, propertyName/*, ID*/));
262 protected virtual bool ControlPropertiesValid()
264 if(ControlToValidate.Length == 0)
266 throw new HttpException(HttpRuntime.FormatResourceString("Validator_control_blank", ID));
268 CheckControlValidationProperty(ControlToValidate, "ControlToValidate");
273 protected virtual bool DetermineRenderUplevel()
276 if(page == null || page.Request == null)
281 if(EnableClientScript)
283 // By now, 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)
292 protected string GetControlRenderID(string name)
294 Control ctrl = FindControl(name);
297 return ctrl.ClientID;
302 protected string GetControlValidationValue(string name)
304 Control ctrl = NamingContainer.FindControl(name);
307 PropertyDescriptor pd = GetValidationProperty(ctrl);
310 object item = pd.GetValue(ctrl);
313 return ((ListItem)item).Value;
315 return item.ToString();
321 protected override void OnInit(EventArgs e)
324 Page.Validators.Add(this);
327 protected override void OnPreRender(EventArgs e)
330 isPreRenderCalled = true;
331 isPropertiesChecked = false;
332 renderUplevel = DetermineRenderUplevel();
335 RegisterValidatorCommonScript();
339 protected override void OnUnload(EventArgs e)
343 Page.Validators.Remove(this);
348 [MonoTODO("Damn_This_Is_Really_Frustrating___by_Gaurav")]
349 protected void RegisterValidatorCommonScript()
351 if(Page.IsClientScriptBlockRegistered("ValidatorIncludeScript"))
354 throw new NotImplementedException();
357 [MonoTODO("I_have_to_know_javascript_for_this_I_know_it_but_for_ALL_browsers_NO")]
358 protected virtual void RegisterValidatorDeclaration()
360 throw new NotImplementedException();
361 //TODO: Since I have to access document.<ClientID> and register
362 // as page validator. Now this is Browser dependent :((
364 // This does not render anything on Mozilla, and does on IE
367 [MonoTODO("Render_ing_always_left")]
368 protected override void Render (HtmlTextWriter writer)
375 if (isPreRenderCalled) {
378 isPropertiesChecked = true;
379 propertiesValid = true;
380 renderUplevel = false;
384 if (PropertiesValid) {
386 Page.VerifyRenderingInServerForm (this);
388 ValidatorDisplay dis = Display;
390 //FIXME: as of now, don't do client-side validation
391 throw new NotImplementedException();
395 RenderBeginTag (writer);
396 if (Text.Trim ().Length > 0 || HasControls ()) {
397 RenderContents (writer);
398 RenderEndTag (writer);
400 writer.Write (ErrorMessage);
402 RenderEndTag (writer);
406 writer.Write (" ");
410 protected abstract bool EvaluateIsValid();