php – Zend表单验证范围日期

前端之家收集整理的这篇文章主要介绍了php – Zend表单验证范围日期前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
谁帮我为Zend Framework创建了一个自定义验证器,它可以检查日期是否在一个范围内?

例:

  1. dateGT = 2011-09-05
  2. dateLT = 2011-07-05

如果表单字段设置为:
dateFieldForm = 2011-08-15
我希望验证器返回true!

如果表单字段设置为:
dateFieldForm = 2011-10-15
我希望验证器返回false!

对不起,未来有大量代码
  1. <?PHP
  2.  
  3. /** @see Zend_Validate_Abstract */
  4. require_once 'Zend/Validate/Abstract.PHP';
  5.  
  6. /**
  7. * @category Zend
  8. * @package Zend_Validate
  9. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  10. * @license http://framework.zend.com/license/new-bsd New BSD License
  11. */
  12. class My_Validate_DateCompare extends Zend_Validate_Abstract
  13. {
  14. /**
  15. * Error codes
  16. * @const string
  17. */
  18. const NOT_SAME = 'notSame';
  19. const MISSING_TOKEN = 'missingToken';
  20. const NOT_LATER = 'notLater';
  21. const NOT_EARLIER = 'notEarlier';
  22. const NOT_BETWEEN = 'notBetween';
  23.  
  24. /**
  25. * Error messages
  26. * @var array
  27. */
  28. protected $_messageTemplates = array(
  29. self::NOT_SAME => "The date '%value%' does not match the required",self::NOT_BETWEEN => "The date is not in the valid range",self::NOT_LATER => "The date '%value%' is not later than the required",self::NOT_EARLIER => "The date '%value%' is not earlier than required",self::MISSING_TOKEN => 'No date was provided to match against',);
  30.  
  31. /**
  32. * @var array
  33. */
  34. protected $_messageVariables = array(
  35. 'token' => '_tokenString'
  36. );
  37.  
  38. /**
  39. * Original token against which to validate
  40. * @var string
  41. */
  42. protected $_tokenString;
  43. protected $_token;
  44. protected $_compare;
  45.  
  46. /**
  47. * Sets validator options
  48. *
  49. * @param mixed $token
  50. * @param mixed $compare
  51. * @return void
  52. */
  53. public function __construct($token = null,$compare = null)
  54. {
  55. if (null !== $token) {
  56. $this->setToken($token);
  57. $this->setCompare($compare);
  58. }
  59. }
  60.  
  61. /**
  62. * Set token against which to compare
  63. *
  64. * @param mixed $token
  65. * @return Zend_Validate_Identical
  66. */
  67. public function setToken($token)
  68. {
  69. $this->_tokenString = (string) $token;
  70. $this->_token = $token;
  71. return $this;
  72. }
  73.  
  74. /**
  75. * Retrieve token
  76. *
  77. * @return string
  78. */
  79. public function getToken()
  80. {
  81. return $this->_token;
  82. }
  83.  
  84. /**
  85. * Set compare against which to compare
  86. *
  87. * @param mixed $compare
  88. * @return Zend_Validate_Identical
  89. */
  90. public function setCompare($compare)
  91. {
  92. $this->_compareString = (string) $compare;
  93. $this->_compare = $compare;
  94. return $this;
  95. }
  96.  
  97. /**
  98. * Retrieve compare
  99. *
  100. * @return string
  101. */
  102. public function getCompare()
  103. {
  104. return $this->_compare;
  105. }
  106.  
  107. /**
  108. * Defined by Zend_Validate_Interface
  109. *
  110. * Returns true if and only if a token has been set and the provided value
  111. * matches that token.
  112. *
  113. * @param mixed $value
  114. * @return boolean
  115. */
  116. public function isValid($value)
  117. {
  118. $this->_setValue((string) $value);
  119. $token = $this->getToken();
  120.  
  121. if ($token === null) {
  122. $this->_error(self::MISSING_TOKEN);
  123. return false;
  124. }
  125.  
  126. $date1 = new Zend_Date($value);
  127. $date2 = new Zend_Date($token);
  128.  
  129. // Not Later
  130. if ($this->getCompare() === true){
  131. if ($date1->compare($date2) < 0 || $date1->equals($date2)) {
  132. $this->_error(self::NOT_LATER);
  133. return false;
  134. }
  135. // Not Earlier
  136. } elseif ($this->getCompare() === false) {
  137. if ($date1->compare($date2) > 0 || $date1->equals($date2)) {
  138. $this->_error(self::NOT_EARLIER);
  139. return false;
  140. }
  141. // Exact Match
  142. } elseif ($this->getCompare() === null) {
  143. if (!$date1->equals($date2)) {
  144. $this->_error(self::NOT_SAME);
  145. return false;
  146. }
  147. // In Range
  148. } else {
  149. $date3 = new Zend_Date($this->getCompare());
  150.  
  151. if ($date1->compare($date2) < 0 || $date1->compare($date3) > 0) {
  152. $this->_error(self::NOT_BETWEEN);
  153. return false;
  154. }
  155. }
  156.  
  157. // Date is valid
  158. return true;
  159. }
  160. }

用法

  1. $element->addValidator(new My_Validate_DateCompare('startdate')); //exact match
  2. $element->addValidator(new My_Validate_DateCompare('startdate',null)); //exact match
  3. $element->addValidator(new My_Validate_DateCompare('startdate','enddate')); //between dates
  4. $element->addValidator(new My_Validate_DateCompare('startdate',true)); //not later
  5. $element->addValidator(new My_Validate_DateCompare('startdate',false)); //not earlier

使用全局设置的日期格式(存储在Zend_Registry(‘Locale’)中).

建议每个案例自定义错误消息.

最新更新:修复了错误的默认可选参数,该参数应为NULL而不是True.更改消息以减少混淆.一些格式和空格.

猜你在找的PHP相关文章