Length.class.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. /**
  3. * Validates the length of a form field.
  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_Length extends Staple_Form_Validator
  24. {
  25. const DEFAULT_ERROR = 'Field does not meet length requirements.';
  26. const MIN_LENGTH_ERROR = 'Minimum Length Not Met';
  27. const MAX_LENGTH_ERROR = 'Maximum Length Exceeded';
  28. protected $min = 0;
  29. protected $max;
  30. /**
  31. * Accepts a maximum length to validate against. Also accepts an optional minimum length.
  32. * Whenever PHP starts supporting method overloading, the variables will be reversed in
  33. * order to make more logical sense.
  34. *
  35. * @param int $max
  36. * @param int $min
  37. */
  38. public function __construct($limit1, $limit2 = NULL, $usermsg = NULL)
  39. {
  40. $this->min = (int)$limit1;
  41. if(isset($limit2))
  42. {
  43. if($limit2 >= $limit1)
  44. {
  45. $this->max = (int)$limit2;
  46. }
  47. else
  48. {
  49. $this->min = (int)$limit2;
  50. $this->max = (int)$limit1;
  51. }
  52. }
  53. parent::__construct($usermsg);
  54. }
  55. /**
  56. * @return the $min
  57. */
  58. public function getMin()
  59. {
  60. return $this->min;
  61. }
  62. /**
  63. * @return the $max
  64. */
  65. public function getMax()
  66. {
  67. return $this->max;
  68. }
  69. /**
  70. * @param int $min
  71. */
  72. public function setMin($min)
  73. {
  74. $this->min = $min;
  75. return $this;
  76. }
  77. /**
  78. * @param int $max
  79. */
  80. public function setMax($max)
  81. {
  82. $this->max = $max;
  83. return $this;
  84. }
  85. /**
  86. * Check for Data Length Validity.
  87. * @param mixed $data
  88. * @return boolean
  89. */
  90. public function check($data)
  91. {
  92. $data = (string)$data;
  93. if(strlen($data) >= $this->min)
  94. {
  95. if(isset($this->max) && strlen($data) <= $this->max)
  96. {
  97. return true;
  98. }
  99. elseif(!isset($this->max))
  100. {
  101. return true;
  102. }
  103. else
  104. {
  105. $this->addError(self::MAX_LENGTH_ERROR);
  106. }
  107. }
  108. else
  109. {
  110. $this->addError(self::MIN_LENGTH_ERROR);
  111. }
  112. //Additionally Add the default error message.
  113. //$this->addError();
  114. return false;
  115. }
  116. /**
  117. * (non-PHPdoc)
  118. * @see Staple_Form_Validator::clientJQuery()
  119. */
  120. public function clientJQuery($fieldType, Staple_Form_Element $field)
  121. {
  122. switch ($fieldType)
  123. {
  124. case 'Staple_Form_SelectElement':
  125. $fieldid = "#{$field->getId()}";
  126. $valstring = "#{$field->getId()} option:selected";
  127. break;
  128. case 'Staple_Form_RadioGroup':
  129. $fieldid = "input:radio[name={$field->getName()}]";
  130. $valstring = "input:radio[name={$field->getName()}]:checked";
  131. break;
  132. case 'Staple_Form_CheckboxElement':
  133. return '';
  134. break;
  135. default:
  136. $fieldid = "#{$field->getId()}";
  137. $valstring = $fieldid;
  138. }
  139. $script = "\t//Length Validator for ".addslashes($field->getLabel())."\n";
  140. $script .= "\tif($('$valstring').val().length > {$this->getMax()} || $('$valstring').val().length < {$this->getMin()})\n";
  141. $script .= "\t{\n";
  142. $script .= "\t\terrors.push('".addslashes($field->getLabel()).": \\n{$this->clientJSError()}\\n');\n";
  143. $script .= "\t\t$('$fieldid').addClass('form_error');\n";
  144. $script .= "\t}\n";
  145. $script .= "\telse {\n";
  146. $script .= "\t\t$('$fieldid').removeClass('form_error');\n";
  147. $script .= "\t}\n";
  148. return $script;
  149. }
  150. }
  151. ?>