BaseValidator.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. //
  2. // Permission is hereby granted, free of charge, to any person obtaining
  3. // a copy of this software and associated documentation files (the
  4. // "Software"), to deal in the Software without restriction, including
  5. // without limitation the rights to use, copy, modify, merge, publish,
  6. // distribute, sublicense, and/or sell copies of the Software, and to
  7. // permit persons to whom the Software is furnished to do so, subject to
  8. // the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be
  11. // included in all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  15. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  17. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  18. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  19. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. //
  21. /**
  22. * Project : Mono
  23. * Namespace : System.Web.UI.MobileControls
  24. * Class : BaseValidator
  25. * Author : Gaurav Vaish
  26. *
  27. * Copyright : 2003 with Gaurav Vaish, and with
  28. * Ximian Inc
  29. */
  30. using System.ComponentModel;
  31. using System.Web.UI;
  32. using System.Web.Mobile;
  33. using System.Web.UI.WebControls;
  34. namespace System.Web.UI.MobileControls
  35. {
  36. public abstract class BaseValidator : TextControl, IValidator
  37. {
  38. private System.Web.UI.WebControls.BaseValidator webBaseValidator;
  39. private bool isValid = true;
  40. protected BaseValidator()
  41. {
  42. StyleReference = "error";
  43. webBaseValidator = CreateWebValidator();
  44. if(webBaseValidator == null)
  45. this.webBaseValidator = new DefaultWebValidator();
  46. Controls.Add(webBaseValidator);
  47. webBaseValidator.Display = ValidatorDisplay.Dynamic;
  48. }
  49. private class DefaultWebValidator
  50. : System.Web.UI.WebControls.BaseValidator
  51. {
  52. protected override bool EvaluateIsValid()
  53. {
  54. return false;
  55. }
  56. }
  57. protected virtual System.Web.UI.WebControls.BaseValidator CreateWebValidator()
  58. {
  59. return null;
  60. }
  61. public string ErrorMessage
  62. {
  63. get
  64. {
  65. return webBaseValidator.ErrorMessage;
  66. }
  67. set
  68. {
  69. webBaseValidator.ErrorMessage = value;
  70. }
  71. }
  72. public bool IsValid
  73. {
  74. get
  75. {
  76. return isValid;
  77. }
  78. set
  79. {
  80. isValid = value;
  81. }
  82. }
  83. public void Validate()
  84. {
  85. if(Visible)
  86. {
  87. Control parent = Parent;
  88. bool visible = true;
  89. while(parent != null)
  90. {
  91. if(!parent.Visible)
  92. {
  93. visible = false;
  94. break;
  95. }
  96. parent = parent.Parent;
  97. }
  98. if(visible)
  99. EvaluateIsValid();
  100. } else
  101. {
  102. IsValid = true;
  103. }
  104. }
  105. protected abstract bool EvaluateIsValid();
  106. public string ControlToValidate
  107. {
  108. get
  109. {
  110. return webBaseValidator.ControlToValidate;
  111. }
  112. set
  113. {
  114. webBaseValidator.ControlToValidate = value;
  115. }
  116. }
  117. public ValidatorDisplay Display
  118. {
  119. get
  120. {
  121. return webBaseValidator.Display;
  122. }
  123. set
  124. {
  125. webBaseValidator.Display = value;
  126. }
  127. }
  128. public override string StyleReference
  129. {
  130. get
  131. {
  132. return base.StyleReference;
  133. }
  134. set
  135. {
  136. base.StyleReference = value;
  137. }
  138. }
  139. public override int VisibleWeight
  140. {
  141. get
  142. {
  143. return 0;
  144. }
  145. }
  146. protected void CheckControlValidationProperty(string name,
  147. string propertyName)
  148. {
  149. Control ctrl = NamingContainer.FindControl(name);
  150. if(ctrl == null)
  151. {
  152. // FIXME
  153. throw new ArgumentException("BaseValidator_ControlNotFound");
  154. }
  155. PropertyDescriptor pd = System.Web.UI.WebControls.BaseValidator.GetValidationProperty(ctrl);
  156. if(pd == null)
  157. {
  158. // FIXME
  159. throw new ArgumentException("BaseValidator_BadControlType");
  160. }
  161. }
  162. protected virtual bool ControlPropertiesValid()
  163. {
  164. string ctrl = ControlToValidate;
  165. if(ctrl.Length == 0)
  166. {
  167. // FIXME
  168. throw new ArgumentException("BaseValidator_ControlToValidateBlank");
  169. }
  170. CheckControlValidationProperty(ctrl, "ControlToValidate");
  171. return true;
  172. }
  173. internal bool EvaluateIsValidInternal()
  174. {
  175. try
  176. {
  177. webBaseValidator.Validate();
  178. } catch(Exception)
  179. {
  180. string thisID = ID;
  181. ID = webBaseValidator.ID;
  182. webBaseValidator.ID = thisID;
  183. try
  184. {
  185. webBaseValidator.Validate();
  186. } finally
  187. {
  188. thisID = ID;
  189. ID = webBaseValidator.ID;
  190. webBaseValidator.ID = thisID;
  191. }
  192. }
  193. return webBaseValidator.IsValid;
  194. }
  195. protected override void OnInit(EventArgs e)
  196. {
  197. Page.Validators.Add(this);
  198. base.OnInit(e);
  199. }
  200. protected override void OnPreRender(EventArgs e)
  201. {
  202. if(MobilePage.ActiveForm == Form)
  203. ControlPropertiesValid();
  204. base.OnPreRender(e);
  205. }
  206. }
  207. }