BaseValidator.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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 Enabled;
  91. }
  92. set
  93. {
  94. 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 ForeColor;
  118. }
  119. set
  120. {
  121. ForeColor = value;
  122. }
  123. }
  124. public bool IsValid
  125. {
  126. get
  127. {
  128. object o = ViewState["IsValid"];
  129. if(o != null)
  130. {
  131. return (bool)o;
  132. }
  133. return true;
  134. }
  135. set
  136. {
  137. ViewState["IsValid"] = value;
  138. }
  139. }
  140. public static PropertyDescriptor GetValidationProperty(object component)
  141. {
  142. ValidationPropertyAttribute attrib = (ValidationPropertyAttribute)((TypeDescriptor.GetAttributes(component))[typeof(ValidationPropertyAttribute)]);
  143. if(attrib != null && attrib.Name != null)
  144. {
  145. return (TypeDescriptor.GetProperties(component, null))[attrib.Name];
  146. }
  147. return null;
  148. }
  149. public void Validate()
  150. {
  151. if(!Visible || (Visible && !Enabled))
  152. {
  153. IsValid = true;
  154. }
  155. Control ctrl = Parent;
  156. while(ctrl != null)
  157. {
  158. if(!ctrl.Visible)
  159. {
  160. IsValid = true;
  161. return;
  162. }
  163. ctrl = ctrl.Parent;
  164. }
  165. isPropertiesChecked = false;
  166. if(!PropertiesValid)
  167. {
  168. IsValid = true;
  169. return;
  170. }
  171. IsValid = EvaluateIsValid();
  172. }
  173. protected bool PropertiesValid
  174. {
  175. get
  176. {
  177. if(!isPropertiesChecked)
  178. {
  179. propertiesValid = ControlPropertiesValid();
  180. isPropertiesChecked = true;
  181. }
  182. return propertiesValid;
  183. }
  184. }
  185. protected bool RenderUplevel
  186. {
  187. get
  188. {
  189. return renderUplevel;
  190. }
  191. }
  192. protected override void AddAttributesToRender(HtmlTextWriter writer)
  193. {
  194. bool enabled = Enabled;
  195. if(!Enabled)
  196. {
  197. Enabled = true;
  198. }
  199. AddAttributesToRender(writer);
  200. if(RenderUplevel)
  201. {
  202. if(ID == null)
  203. {
  204. writer.AddAttribute("id", ClientID);
  205. }
  206. if(ControlToValidate.Length > 0)
  207. {
  208. writer.AddAttribute("controltovalidate", GetControlRenderID(ControlToValidate));
  209. }
  210. if(ErrorMessage.Length > 0)
  211. {
  212. writer.AddAttribute("errormessage", ErrorMessage, true);
  213. }
  214. if(Display == ValidatorDisplay.Static)
  215. {
  216. writer.AddAttribute("display", Enum.Format(typeof(ValidatorDisplay), Display, "G").Replace('_','-'));
  217. //writer.AddAttribute("display", PropertyConverter.EnumToString(typeof(ValidatorDisplay), Display));
  218. }
  219. if(!IsValid)
  220. {
  221. writer.AddAttribute("isvalid", "False");
  222. }
  223. if(!enabled)
  224. {
  225. writer.AddAttribute("enabled", "False");
  226. }
  227. }
  228. if(!enabled)
  229. {
  230. Enabled = false;
  231. }
  232. }
  233. [MonoTODO]
  234. protected void CheckControlValidationProperty(string name, string propertyName)
  235. {
  236. Control ctrl = NamingContainer.FindControl(name);
  237. if(ctrl == null)
  238. {
  239. throw new HttpException(HttpRuntime.FormatResourceString("Validator_control_not_found",
  240. name, propertyName/*, ID*/));
  241. }
  242. PropertyDescriptor pd = GetValidationProperty(ctrl);
  243. if(pd == null)
  244. {
  245. throw new HttpException(HttpRuntime.FormatResourceString("Validator_bad_control_type",
  246. name, propertyName/*, ID*/));
  247. }
  248. }
  249. protected virtual bool ControlPropertiesValid()
  250. {
  251. if(ControlToValidate.Length == 0)
  252. {
  253. throw new HttpException(HttpRuntime.FormatResourceString("Validator_control_blank", ID));
  254. }
  255. CheckControlValidationProperty(ControlToValidate, "ControlToValidate");
  256. return true;
  257. }
  258. [MonoTODO]
  259. protected virtual bool DetermineRenderUplevel()
  260. {
  261. Page page = Page;
  262. if(page == null || page.Request == null)
  263. {
  264. return false;
  265. }
  266. if(EnableClientScript)
  267. {
  268. throw new NotImplementedException();
  269. //TODO: I need to get the (Browser->Dom_version_major >= 4 &&
  270. // Brower->Ecma_script_version >= 1.2)
  271. }
  272. return false;
  273. }
  274. protected string GetControlRenderID(string name)
  275. {
  276. Control ctrl = FindControl(name);
  277. if(ctrl != null)
  278. {
  279. return ctrl.ClientID;
  280. }
  281. return String.Empty;
  282. }
  283. protected string GetControlValidationValue(string name)
  284. {
  285. Control ctrl = NamingContainer.FindControl(name);
  286. if(ctrl != null)
  287. {
  288. PropertyDescriptor pd = GetValidationProperty(ctrl);
  289. if(pd != null)
  290. {
  291. object item = pd.GetValue(ctrl);
  292. if(item is ListItem)
  293. {
  294. return ((ListItem)item).Value;
  295. }
  296. return item.ToString();
  297. }
  298. }
  299. return null;
  300. }
  301. protected override void OnInit(EventArgs e)
  302. {
  303. OnInit(e);
  304. Page.Validators.Add(this);
  305. }
  306. protected override void OnPreRender(EventArgs e)
  307. {
  308. OnPreRender(e);
  309. isPreRenderCalled = true;
  310. isPropertiesChecked = false;
  311. renderUplevel = DetermineRenderUplevel();
  312. if(renderUplevel)
  313. {
  314. RegisterValidatorCommonScript();
  315. }
  316. }
  317. protected override void OnUnload(EventArgs e)
  318. {
  319. if(Page != null)
  320. {
  321. Page.Validators.Remove(this);
  322. }
  323. OnUnload(e);
  324. }
  325. [MonoTODO("What_do_I_have_to_do")]
  326. protected void RegisterValidatorCommonScript()
  327. {
  328. throw new NotImplementedException();
  329. }
  330. [MonoTODO("I_have_to_know_javascript_for_this_I_know_it_but_for_ALL_browsers_NO")]
  331. protected virtual void RegisterValidatorDeclaration()
  332. {
  333. throw new NotImplementedException();
  334. //TODO: Since I have to access document.<ClientID> and register
  335. // as page validator. Now this is Browser dependent :((
  336. }
  337. [MonoTODO("Render_ing_always_left")]
  338. protected override void Render(HtmlTextWriter writer)
  339. {
  340. bool valid;
  341. if(isPreRenderCalled)
  342. {
  343. valid = (Enabled && IsValid);
  344. } else
  345. {
  346. isPropertiesChecked = true;
  347. propertiesValid = true;
  348. renderUplevel = false;
  349. valid = true;
  350. }
  351. if(PropertiesValid)
  352. {
  353. if(Page != null)
  354. {
  355. Page.VerifyRenderingInServerForm(this);
  356. }
  357. ValidatorDisplay dis = Display;
  358. if(RenderUplevel)
  359. {
  360. throw new NotImplementedException();
  361. }
  362. throw new NotImplementedException();
  363. }
  364. return;
  365. }
  366. protected abstract bool EvaluateIsValid();
  367. }
  368. }