InArray.class.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /**
  3. * Validates that the supplied value is within an array of valid values.
  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_InArray extends Staple_Form_Validator
  24. {
  25. const DEFAULT_ERROR = 'Supplied data not in accepted list of values.';
  26. /**
  27. * Valid array values.
  28. * @var array
  29. */
  30. protected $arrayvalues = array();
  31. /**
  32. * Supply an array to the constructor to define valid options.
  33. * @param array $values
  34. */
  35. function __construct(array $values = array(), $usermsg = NULL)
  36. {
  37. $this->arrayvalues = $values;
  38. parent::__construct($usermsg);
  39. }
  40. /**
  41. * Add a new value to the valid array list.
  42. * @param mixed $value
  43. */
  44. public function addValue($value)
  45. {
  46. if(is_array($value))
  47. {
  48. $this->arrayvalues = array_merge($this->arrayvalues,$value);
  49. }
  50. else
  51. {
  52. $this->arrayvalues[] = $value;
  53. }
  54. }
  55. /**
  56. * Check that the supplied data exists as a value in the array;
  57. * @param mixed $data
  58. * @return bool
  59. * @see Staple_Form_Validator::check()
  60. */
  61. public function check($data)
  62. {
  63. if(in_array($data, $this->arrayvalues) === true)
  64. {
  65. return true;
  66. }
  67. else
  68. {
  69. $this->addError();
  70. }
  71. return false;
  72. }
  73. /**
  74. * @see Staple_Form_Validator::clientJQuery()
  75. */
  76. public function clientJQuery($fieldType, Staple_Form_Element $field)
  77. {
  78. switch ($fieldType)
  79. {
  80. case 'Staple_Form_SelectElement':
  81. $fieldid = "#{$field->getId()}";
  82. $valstring = "#{$field->getId()} option:selected";
  83. break;
  84. case 'Staple_Form_RadioGroup':
  85. $fieldid = "input:radio[name={$field->getName()}]";
  86. $valstring = "input:radio[name={$field->getName()}]:checked";
  87. break;
  88. case 'Staple_Form_CheckboxElement':
  89. return '';
  90. break;
  91. default:
  92. $fieldid = "#{$field->getId()}";
  93. $valstring = $fieldid;
  94. }
  95. $script = "\t//Selection Validator for ".addslashes($field->getLabel())."\n";
  96. $script .= "\tif(-1 == $.inArray($('$valstring').val(),[";
  97. foreach($this->arrayvalues as $value)
  98. {
  99. $script .= "'$value',";
  100. }
  101. $script = substr($script, 0,strlen($script)-1);
  102. $script .= "]))\n\t{\n";
  103. $script .= "\t\terrors.push('".addslashes($field->getLabel()).": \\n{$this->clientJSError()}\\n');\n";
  104. $script .= "\t\t$('$fieldid').addClass('form_error');\n";
  105. $script .= "\t}\n";
  106. $script .= "\telse {\n";
  107. $script .= "\t\t$('$fieldid').removeClass('form_error');\n";
  108. $script .= "\t}\n";
  109. return $script;
  110. }
  111. /**
  112. * @see Staple_Form_Validator::clientJS()
  113. */
  114. public function clientJS($fieldType, Staple_Form_Element $field)
  115. {
  116. // TODO Auto-generated method stub
  117. }
  118. }
  119. ?>