BaseValidator.cs 8.4 KB

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