BaseValidator.cs 8.2 KB

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