BaseValidator.cs 7.7 KB

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