Equal.class.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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_Equal extends Staple_Form_Validator
  22. {
  23. const DEFAULT_ERROR = 'Data is not equal';
  24. protected $strict;
  25. protected $equal;
  26. public function __construct($equal, $strict = false, $usermsg = NULL)
  27. {
  28. $this->equal = $equal;
  29. $this->strict = (bool)$strict;
  30. parent::__construct($usermsg);
  31. }
  32. /**
  33. *
  34. * @param mixed $data
  35. * @return bool
  36. * @see Staple_Form_Validator::check()
  37. */
  38. public function check($data)
  39. {
  40. if($this->strict === true)
  41. {
  42. if($this->equal === $data)
  43. {
  44. return true;
  45. }
  46. else
  47. {
  48. $this->addError();
  49. }
  50. }
  51. else
  52. {
  53. if($this->equal == $data)
  54. {
  55. return true;
  56. }
  57. else
  58. {
  59. $this->addError();
  60. }
  61. }
  62. return false;
  63. }
  64. /**
  65. * (non-PHPdoc)
  66. * @see Staple_Form_Validator::clientJQuery()
  67. */
  68. public function clientJQuery($fieldType, Staple_Form_Element $field)
  69. {
  70. switch ($fieldType)
  71. {
  72. case 'Staple_Form_SelectElement':
  73. $fieldid = "#{$field->getId()}";
  74. $valstring = "#{$field->getId()} option:selected";
  75. break;
  76. case 'Staple_Form_RadioGroup':
  77. $fieldid = "input:radio[name={$field->getName()}]";
  78. $valstring = "input:radio[name={$field->getName()}]:checked";
  79. break;
  80. case 'Staple_Form_CheckboxElement':
  81. return '';
  82. break;
  83. default:
  84. $fieldid = "#{$field->getId()}";
  85. $valstring = $fieldid;
  86. }
  87. $script = "\t//Equal Validator for ".addslashes($field->getLabel())."\n";
  88. $script .= "\tif(!($('$valstring').val() == '".addslashes($this->equal)."'))\n\t{\n";
  89. $script .= "\t\terrors.push('".addslashes($field->getLabel()).": \\n{$this->clientJSError()}\\n');\n";
  90. $script .= "\t\t$('$fieldid').addClass('form_error');\n";
  91. $script .= "\t}\n";
  92. $script .= "\telse {\n";
  93. $script .= "\t\t$('$fieldid').removeClass('form_error');\n";
  94. $script .= "\t}\n";
  95. return $script;
  96. }
  97. }
  98. ?>