2 // System.Web.UI.WebControls.BaseValidator.cs
5 // Gaurav Vaish (gvaish@iitk.ac.in)
6 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // Andreas Nahr (ClassDevelopment@A-SoftTech.com)
9 // (c) 2002 Ximian, Inc. (http://www.ximian.com)
10 // (C) Gaurav Vaish (2002)
11 // (C) 2003 Andreas Nahr
15 using System.ComponentModel;
16 using System.ComponentModel.Design;
21 namespace System.Web.UI.WebControls
23 [DefaultProperty("ErrorMessage")]
24 [Designer ("System.Web.UI.Design.WebControls.BaseValidatorDesigner, " + Consts.AssemblySystem_Design, typeof (IDesigner))]
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 [DefaultValue (""), WebCategory ("Behavior")]
43 [TypeConverter (typeof (ValidatedControlConverter))]
44 [WebSysDescription ("The ID of the control to validate.")]
45 public string ControlToValidate
49 object o = ViewState["ControlToValidate"];
58 ViewState["ControlToValidate"] = value;
62 [DefaultValue (typeof (ValidatorDisplay), "Static"), Bindable (true), WebCategory ("Appearance")]
63 [WebSysDescription ("Determines how the validator is displayed.")]
64 public ValidatorDisplay Display
68 object o = ViewState["Display"];
71 return (ValidatorDisplay)o;
73 return ValidatorDisplay.Static;
77 if(!Enum.IsDefined(typeof(ValidatorDisplay), value))
79 throw new ArgumentException();
81 ViewState["ValidatorDisplay"] = value;
85 [DefaultValue (true), WebCategory ("Behavior")]
86 [WebSysDescription ("Determines if client script is activated on uplevel browsers.")]
87 public bool EnableClientScript
91 object o = ViewState["EnableClientScript"];
100 ViewState["EnableClientScript"] = value;
104 public override bool Enabled
114 base.Enabled = value;
118 [DefaultValue (""), Bindable (true), WebCategory ("Appearance")]
119 [WebSysDescription ("An error message that is displayed if the control validates to false.")]
120 public string ErrorMessage
124 object o = ViewState["ErrorMessage"];
133 ViewState["ErrorMessage"] = value;
137 [DefaultValue (null)]
138 public override Color ForeColor
142 return base.ForeColor;
146 base.ForeColor = value;
150 [DefaultValue (true), Browsable (false), WebCategory ("Misc")]
151 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
152 [WebSysDescription ("Indicates if the control validated to false.")]
155 get { return isValid; }
156 set { isValid = value; }
159 public static PropertyDescriptor GetValidationProperty(object component)
161 System.ComponentModel.AttributeCollection coll = TypeDescriptor.GetAttributes (component);
162 Type type = typeof (ValidationPropertyAttribute);
163 ValidationPropertyAttribute attrib = (ValidationPropertyAttribute) coll [type];
164 if (attrib != null && attrib.Name != null)
165 return (TypeDescriptor.GetProperties (component)) [attrib.Name];
169 public void Validate()
171 if(!Visible || (Visible && !Enabled))
175 Control ctrl = Parent;
185 isPropertiesChecked = false;
191 IsValid = EvaluateIsValid();
194 protected bool PropertiesValid
198 if(!isPropertiesChecked)
200 propertiesValid = ControlPropertiesValid();
201 isPropertiesChecked = true;
203 return propertiesValid;
207 protected bool RenderUplevel
211 return renderUplevel;
215 protected override void AddAttributesToRender(HtmlTextWriter writer)
217 bool enabled = base.Enabled;
221 base.AddAttributesToRender(writer);
226 writer.AddAttribute("id", ClientID);
228 if(ControlToValidate.Length > 0)
230 writer.AddAttribute("controltovalidate", GetControlRenderID(ControlToValidate));
232 if(ErrorMessage.Length > 0)
234 writer.AddAttribute("errormessage", ErrorMessage, true);
236 if(Display == ValidatorDisplay.Static)
238 writer.AddAttribute("display", Enum.Format(typeof(ValidatorDisplay), Display, "G").Replace('_','-'));
239 //writer.AddAttribute("display", PropertyConverter.EnumToString(typeof(ValidatorDisplay), Display));
243 writer.AddAttribute("isvalid", "False");
246 ControlStyle.AddAttributesToRender (writer, this);
249 writer.AddAttribute("enabled", "False");
259 protected void CheckControlValidationProperty(string name, string propertyName)
261 Control ctrl = NamingContainer.FindControl(name);
264 throw new HttpException(HttpRuntime.FormatResourceString("Validator_control_not_found",
265 name, propertyName/*, ID*/));
267 PropertyDescriptor pd = GetValidationProperty(ctrl);
270 throw new HttpException(HttpRuntime.FormatResourceString("Validator_bad_control_type",
271 name, propertyName/*, ID*/));
275 protected virtual bool ControlPropertiesValid()
277 if(ControlToValidate.Length == 0)
279 throw new HttpException(HttpRuntime.FormatResourceString("Validator_control_blank", ID));
281 CheckControlValidationProperty(ControlToValidate, "ControlToValidate");
286 protected virtual bool DetermineRenderUplevel()
289 if(page == null || page.Request == null)
294 if(EnableClientScript)
296 // By now, return false
298 //throw new NotImplementedException();
299 ////TODO: I need to get the (Browser->Dom_version_major >= 4 &&
300 //// Brower->Ecma_script_version >= 1.2)
305 protected string GetControlRenderID(string name)
307 Control ctrl = FindControl(name);
310 return ctrl.ClientID;
315 protected string GetControlValidationValue(string name)
317 Control ctrl = NamingContainer.FindControl(name);
320 PropertyDescriptor pd = GetValidationProperty(ctrl);
323 object item = pd.GetValue (ctrl);
324 if (item is ListItem)
325 return ((ListItem) item).Value;
330 return item.ToString ();
336 protected override void OnInit(EventArgs e)
339 Page.Validators.Add(this);
342 protected override void OnPreRender(EventArgs e)
345 isPreRenderCalled = true;
346 isPropertiesChecked = false;
347 renderUplevel = DetermineRenderUplevel();
350 RegisterValidatorCommonScript();
354 protected override void OnUnload(EventArgs e)
358 Page.Validators.Remove(this);
363 [MonoTODO("Damn_This_Is_Really_Frustrating___by_Gaurav")]
364 protected void RegisterValidatorCommonScript()
366 if(Page.IsClientScriptBlockRegistered("ValidatorIncludeScript"))
369 throw new NotImplementedException();
372 [MonoTODO("I_have_to_know_javascript_for_this_I_know_it_but_for_ALL_browsers_NO")]
373 protected virtual void RegisterValidatorDeclaration()
375 throw new NotImplementedException();
376 //TODO: Since I have to access document.<ClientID> and register
377 // as page validator. Now this is Browser dependent :((
379 // This does not render anything on Mozilla, and does on IE
382 [MonoTODO("Render_ing_always_left")]
383 protected override void Render (HtmlTextWriter writer)
390 if (isPreRenderCalled) {
393 isPropertiesChecked = true;
394 propertiesValid = true;
395 renderUplevel = false;
399 if (PropertiesValid) {
401 Page.VerifyRenderingInServerForm (this);
403 ValidatorDisplay dis = Display;
405 //FIXME: as of now, don't do client-side validation
406 throw new NotImplementedException();
410 RenderBeginTag (writer);
411 if (Text.Trim ().Length > 0 || HasControls ()) {
412 RenderContents (writer);
413 RenderEndTag (writer);
415 writer.Write (ErrorMessage);
417 RenderEndTag (writer);
421 writer.Write (" ");
425 protected abstract bool EvaluateIsValid();