BaseValidator.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /**
  2. * Project : Mono
  3. * Namespace : System.Web.UI.MobileControls
  4. * Class : BaseValidator
  5. * Author : Gaurav Vaish
  6. *
  7. * Copyright : 2003 with Gaurav Vaish, and with
  8. * Ximian Inc
  9. */
  10. using System.ComponentModel;
  11. using System.Web.UI;
  12. using System.Web.Mobile;
  13. using System.Web.UI.WebControls;
  14. namespace System.Web.UI.MobileControls
  15. {
  16. public abstract class BaseValidator : TextControl, IValidator
  17. {
  18. private System.Web.UI.WebControls.BaseValidator webBaseValidator;
  19. private bool isValid = true;
  20. protected BaseValidator()
  21. {
  22. StyleReference = "error";
  23. webBaseValidator = CreateWebValidator();
  24. if(webBaseValidator == null)
  25. this.webBaseValidator = new DefaultWebValidator();
  26. Controls.Add(webBaseValidator);
  27. webBaseValidator.Display = ValidatorDisplay.Dynamic;
  28. }
  29. private class DefaultWebValidator
  30. : System.Web.UI.WebControls.BaseValidator
  31. {
  32. protected override bool EvaluateIsValid()
  33. {
  34. return false;
  35. }
  36. }
  37. protected virtual System.Web.UI.WebControls.BaseValidator CreateWebValidator()
  38. {
  39. return null;
  40. }
  41. public string ErrorMessage
  42. {
  43. get
  44. {
  45. return webBaseValidator.ErrorMessage;
  46. }
  47. set
  48. {
  49. webBaseValidator.ErrorMessage = value;
  50. }
  51. }
  52. public bool IsValid
  53. {
  54. get
  55. {
  56. return isValid;
  57. }
  58. set
  59. {
  60. isValid = value;
  61. }
  62. }
  63. public void Validate()
  64. {
  65. if(Visible)
  66. {
  67. Control parent = Parent;
  68. bool visible = true;
  69. while(parent != null)
  70. {
  71. if(!parent.Visible)
  72. {
  73. visible = false;
  74. break;
  75. }
  76. parent = parent.Parent;
  77. }
  78. if(visible)
  79. EvaluateIsValid();
  80. } else
  81. {
  82. IsValid = true;
  83. }
  84. }
  85. protected abstract bool EvaluateIsValid();
  86. public string ControlToValidate
  87. {
  88. get
  89. {
  90. return webBaseValidator.ControlToValidate;
  91. }
  92. set
  93. {
  94. webBaseValidator.ControlToValidate = value;
  95. }
  96. }
  97. public ValidatorDisplay Display
  98. {
  99. get
  100. {
  101. return webBaseValidator.Display;
  102. }
  103. set
  104. {
  105. webBaseValidator.Display = value;
  106. }
  107. }
  108. public override string StyleReference
  109. {
  110. get
  111. {
  112. return base.StyleReference;
  113. }
  114. set
  115. {
  116. base.StyleReference = value;
  117. }
  118. }
  119. public override int VisibleWeight
  120. {
  121. get
  122. {
  123. return 0;
  124. }
  125. }
  126. protected void CheckControlValidationProperty(string name,
  127. string propertyName)
  128. {
  129. Control ctrl = NamingContainer.FindControl(name);
  130. if(ctrl == null)
  131. {
  132. // FIXME
  133. throw new ArgumentException("BaseValidator_ControlNotFound");
  134. }
  135. PropertyDescriptor pd = System.Web.UI.WebControls.BaseValidator.GetValidationProperty(ctrl);
  136. if(pd == null)
  137. {
  138. // FIXME
  139. throw new ArgumentException("BaseValidator_BadControlType");
  140. }
  141. }
  142. protected virtual bool ControlPropertiesValid()
  143. {
  144. string ctrl = ControlToValidate;
  145. if(ctrl.Length == 0)
  146. {
  147. // FIXME
  148. throw new ArgumentException("BaseValidator_ControlToValidateBlank");
  149. }
  150. CheckControlValidationProperty(ctrl, "ControlToValidate");
  151. return true;
  152. }
  153. internal bool EvaluateIsValidInternal()
  154. {
  155. try
  156. {
  157. webBaseValidator.Validate();
  158. } catch(Exception)
  159. {
  160. string thisID = ID;
  161. ID = webBaseValidator.ID;
  162. webBaseValidator.ID = thisID;
  163. try
  164. {
  165. webBaseValidator.Validate();
  166. } finally
  167. {
  168. thisID = ID;
  169. ID = webBaseValidator.ID;
  170. webBaseValidator.ID = thisID;
  171. }
  172. }
  173. return webBaseValidator.IsValid;
  174. }
  175. protected override void OnInit(EventArgs e)
  176. {
  177. Page.Validators.Add(this);
  178. base.OnInit(e);
  179. }
  180. protected override void OnPreRender(EventArgs e)
  181. {
  182. if(MobilePage.ActiveForm == Form)
  183. ControlPropertiesValid();
  184. base.OnPreRender(e);
  185. }
  186. }
  187. }