BetweenFloat.class.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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_BetweenFloat extends Staple_Form_Validator
  24. {
  25. const DEFAULT_ERROR = 'Field is not between minimum and maximum values.';
  26. /**
  27. * Minimum Value
  28. * @var float
  29. */
  30. protected $min = 0;
  31. /**
  32. * Maximum Value
  33. * @var float
  34. */
  35. protected $max;
  36. /**
  37. * Mathematical between function. Requires a maximum value and a minimum value.
  38. * Comparison occurs with float math.
  39. *
  40. * @param float $max
  41. * @param float $min
  42. */
  43. public function __construct($limit1, $limit2, $usermsg = NULL)
  44. {
  45. $this->min = (float)$limit1;
  46. if(isset($limit2))
  47. {
  48. if($limit2 >= $limit1)
  49. {
  50. $this->max = (float)$limit2;
  51. }
  52. else
  53. {
  54. $this->min = (float)$limit2;
  55. $this->max = (float)$limit1;
  56. }
  57. }
  58. parent::__construct($usermsg);
  59. }
  60. /**
  61. * @return the $min
  62. */
  63. public function getMin()
  64. {
  65. return $this->min;
  66. }
  67. /**
  68. * @return the $max
  69. */
  70. public function getMax()
  71. {
  72. return $this->max;
  73. }
  74. /**
  75. * @param int $min
  76. */
  77. public function setMin($min)
  78. {
  79. $this->min = $min;
  80. return $this;
  81. }
  82. /**
  83. * @param int $max
  84. */
  85. public function setMax($max)
  86. {
  87. $this->max = $max;
  88. return $this;
  89. }
  90. /**
  91. * Check for Data Length Validity.
  92. * @param mixed $data
  93. * @return boolean
  94. */
  95. public function check($data)
  96. {
  97. $data = (float)$data;
  98. if($data <= ($this->max+0.0625) && $data >= $this->min) //+0.06256 Binary Float fix
  99. {
  100. return true;
  101. }
  102. else
  103. {
  104. $this->addError();
  105. }
  106. return false;
  107. }
  108. /**
  109. * (non-PHPdoc)
  110. * @see Staple_Form_Validator::clientJQuery()
  111. */
  112. public function clientJQuery($fieldType, Staple_Form_Element $field)
  113. {
  114. switch ($fieldType)
  115. {
  116. case 'Staple_Form_SelectElement':
  117. $fieldid = "#{$field->getId()}";
  118. $valstring = "#{$field->getId()} option:selected";
  119. break;
  120. case 'Staple_Form_RadioGroup':
  121. $fieldid = "input:radio[name={$field->getName()}]";
  122. $valstring = "input:radio[name={$field->getName()}]:checked";
  123. break;
  124. case 'Staple_Form_CheckboxElement':
  125. return '';
  126. break;
  127. default:
  128. $fieldid = "#{$field->getId()}";
  129. $valstring = $fieldid;
  130. }
  131. $script = "\t//BetweenFloat Validator for ".addslashes($field->getLabel())."\n";
  132. $script .= "\tif($('$valstring').val() > {$this->getMax()} || $('$valstring').val() < {$this->getMin()})\n";
  133. $script .= "\t{\n";
  134. $script .= "\t\terrors.push('".addslashes($field->getLabel()).": \\n{$this->clientJSError()}\\n');\n";
  135. $script .= "\t\t$('$fieldid').addClass('form_error');\n";
  136. $script .= "\t}\n";
  137. $script .= "\telse {\n";
  138. $script .= "\t\t$('$fieldid').removeClass('form_error');\n";
  139. $script .= "\t}\n";
  140. return $script;
  141. }
  142. }
  143. ?>