Between.class.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /**
  3. * Validates a numeric value is between the min and the max.
  4. *
  5. * @author Ironpilot
  6. * @copyright Copywrite (c) 2011, STAPLE CODE
  7. *
  8. * This file is part of the STAPLE Framework.
  9. *
  10. * The STAPLE Framework is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Lesser General Public License as published by the
  12. * Free Software Foundation, either version 3 of the License, or (at your option)
  13. * any later version.
  14. *
  15. * The STAPLE Framework is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  17. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
  18. * more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public License
  21. * along with the STAPLE Framework. If not, see <http://www.gnu.org/licenses/>.
  22. */
  23. class Staple_Form_Validate_Between extends Staple_Form_Validator
  24. {
  25. const DEFAULT_ERROR = 'Field is not between minimum and maximum values.';
  26. protected $min = 0;
  27. protected $max;
  28. /**
  29. * Mathematical between function. Requires a maximum value and a minimum value.
  30. * Comparison occurs with integer math.
  31. *
  32. * @param int $max
  33. * @param int $min
  34. */
  35. public function __construct($limit1, $limit2, $usermsg = NULL)
  36. {
  37. $this->min = (int)$limit1;
  38. if(isset($limit2))
  39. {
  40. if($limit2 >= $limit1)
  41. {
  42. $this->max = (int)$limit2;
  43. }
  44. else
  45. {
  46. $this->min = (int)$limit2;
  47. $this->max = (int)$limit1;
  48. }
  49. }
  50. parent::__construct($usermsg);
  51. }
  52. /**
  53. * @return the $min
  54. */
  55. public function getMin()
  56. {
  57. return $this->min;
  58. }
  59. /**
  60. * @return the $max
  61. */
  62. public function getMax()
  63. {
  64. return $this->max;
  65. }
  66. /**
  67. * @param int $min
  68. */
  69. public function setMin($min)
  70. {
  71. $this->min = $min;
  72. return $this;
  73. }
  74. /**
  75. * @param int $max
  76. */
  77. public function setMax($max)
  78. {
  79. $this->max = $max;
  80. return $this;
  81. }
  82. /**
  83. * Check for Data Length Validity.
  84. * @param mixed $data
  85. * @return boolean
  86. */
  87. public function check($data)
  88. {
  89. $data = (int)$data;
  90. if($data <= $this->max && $data >= $this->min)
  91. {
  92. return true;
  93. }
  94. else
  95. {
  96. $this->addError();
  97. }
  98. return false;
  99. }
  100. /**
  101. * (non-PHPdoc)
  102. * @see Staple_Form_Validator::clientJQuery()
  103. */
  104. public function clientJQuery($fieldType, Staple_Form_Element $field)
  105. {
  106. switch ($fieldType)
  107. {
  108. case 'Staple_Form_SelectElement':
  109. $fieldid = "#{$field->getId()}";
  110. $valstring = "#{$field->getId()} option:selected";
  111. break;
  112. case 'Staple_Form_RadioGroup':
  113. $fieldid = "input:radio[name={$field->getName()}]";
  114. $valstring = "input:radio[name={$field->getName()}]:checked";
  115. break;
  116. case 'Staple_Form_CheckboxElement':
  117. return '';
  118. break;
  119. default:
  120. $fieldid = "#{$field->getId()}";
  121. $valstring = $fieldid;
  122. }
  123. $script = "\t//Between Validator for ".addslashes($field->getLabel())."\n";
  124. $script .= "\tif($('$valstring').val() > {$this->getMax()} || $('$valstring').val() < {$this->getMin()})\n";
  125. $script .= "\t{\n";
  126. $script .= "\t\terrors.push('".addslashes($field->getLabel()).": \\n{$this->clientJSError()}\\n');\n";
  127. $script .= "\t\t$('$fieldid').addClass('form_error');\n";
  128. $script .= "\t}\n";
  129. $script .= "\telse {\n";
  130. $script .= "\t\t$('$fieldid').removeClass('form_error');\n";
  131. $script .= "\t}\n";
  132. return $script;
  133. }
  134. }
  135. ?>