NotEqual.class.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /**
  3. * @author Ironpilot
  4. * @copyright Copywrite (c) 2011, STAPLE CODE
  5. *
  6. * This file is part of the STAPLE Framework.
  7. *
  8. * The STAPLE Framework is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Lesser General Public License as published by the
  10. * Free Software Foundation, either version 3 of the License, or (at your option)
  11. * any later version.
  12. *
  13. * The STAPLE Framework is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  15. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
  16. * more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public License
  19. * along with the STAPLE Framework. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. class Staple_Form_Validate_NotEqual extends Staple_Form_Validator
  22. {
  23. const DEFAULT_ERROR = 'Data matches an excluded value.';
  24. protected $equal;
  25. public function __construct($equal, $usermsg = NULL)
  26. {
  27. $this->equal = $equal;
  28. parent::__construct($usermsg);
  29. }
  30. /**
  31. *
  32. * @param mixed $data
  33. * @return bool
  34. * @see Staple_Form_Validator::check()
  35. */
  36. public function check($data)
  37. {
  38. if($this->equal != $data)
  39. {
  40. return true;
  41. }
  42. else
  43. {
  44. $this->addError();
  45. }
  46. return false;
  47. }
  48. /**
  49. * (non-PHPdoc)
  50. * @see Staple_Form_Validator::clientJQuery()
  51. */
  52. public function clientJQuery($fieldType, Staple_Form_Element $field)
  53. {
  54. switch ($fieldType)
  55. {
  56. case 'Staple_Form_SelectElement':
  57. $fieldid = "#{$field->getId()}";
  58. $valstring = "#{$field->getId()} option:selected";
  59. break;
  60. case 'Staple_Form_RadioGroup':
  61. $fieldid = "input:radio[name={$field->getName()}]";
  62. $valstring = "input:radio[name={$field->getName()}]:checked";
  63. break;
  64. case 'Staple_Form_CheckboxElement':
  65. return '';
  66. break;
  67. default:
  68. $fieldid = "#{$field->getId()}";
  69. $valstring = $fieldid;
  70. }
  71. $script = "\t//NotEqual Validator for ".addslashes($field->getLabel())."\n";
  72. $script .= "\tif($('$valstring').val() == '".addslashes($this->equal)."')\n\t{\n";
  73. $script .= "\t\terrors.push('".addslashes($field->getLabel()).": \\n{$this->clientJSError()}\\n');\n";
  74. $script .= "\t\t$('$fieldid').addClass('form_error');\n";
  75. $script .= "\t}\n";
  76. $script .= "\telse {\n";
  77. $script .= "\t\t$('$fieldid').removeClass('form_error');\n";
  78. $script .= "\t}\n";
  79. return $script;
  80. }
  81. }
  82. ?>