IdenticalField.class.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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_IdenticalField extends Staple_Form_Validator
  22. {
  23. const DEFAULT_ERROR = 'Data is not equal';
  24. protected $strict = false;
  25. /**
  26. * The form element to validate against.
  27. * @var Staple_Form_Element
  28. */
  29. protected $field;
  30. public function __construct(Staple_Form_Element $field = NULL, $strict = false, $usermsg = NULL)
  31. {
  32. if(isset($field))
  33. {
  34. $this->setField($field);
  35. }
  36. $this->strict = (bool)$strict;
  37. parent::__construct($usermsg);
  38. }
  39. /**
  40. *
  41. * @param mixed $data
  42. * @return bool
  43. * @see Staple_Form_Validator::check()
  44. */
  45. public function check($data)
  46. {
  47. if($this->strict === true)
  48. {
  49. if($this->field->getValue() === $data)
  50. {
  51. return true;
  52. }
  53. else
  54. {
  55. $this->addError();
  56. }
  57. }
  58. else
  59. {
  60. if($this->field->getValue() == $data)
  61. {
  62. return true;
  63. }
  64. else
  65. {
  66. $this->addError();
  67. }
  68. }
  69. return false;
  70. }
  71. /**
  72. * @return the $strict
  73. */
  74. public function getStrict()
  75. {
  76. return $this->strict;
  77. }
  78. /**
  79. * @return Staple_Form_Element $field
  80. */
  81. public function getField()
  82. {
  83. return $this->field;
  84. }
  85. /**
  86. * @param boolean $strict
  87. */
  88. public function setStrict($strict)
  89. {
  90. $this->strict = (bool)$strict;
  91. return $this;
  92. }
  93. /**
  94. * @param Staple_Form_Element $field
  95. */
  96. public function setField(Staple_Form_Element $field)
  97. {
  98. $this->field = $field;
  99. return $this;
  100. }
  101. /**
  102. * (non-PHPdoc)
  103. * @see Staple_Form_Validator::clientJQuery()
  104. */
  105. public function clientJQuery($fieldType, Staple_Form_Element $field)
  106. {
  107. switch ($fieldType)
  108. {
  109. case 'Staple_Form_SelectElement':
  110. $fieldid = "#{$field->getId()}";
  111. $valstring = "#{$field->getId()} option:selected";
  112. break;
  113. case 'Staple_Form_RadioGroup':
  114. $fieldid = "input:radio[name={$field->getName()}]";
  115. $valstring = "input:radio[name={$field->getName()}]:checked";
  116. break;
  117. case 'Staple_Form_CheckboxElement':
  118. return '';
  119. break;
  120. default:
  121. $fieldid = "#{$field->getId()}";
  122. $valstring = $fieldid;
  123. }
  124. switch (get_class($this->field))
  125. {
  126. case 'Staple_Form_SelectElement':
  127. $identstring = "#{$field->getId()} option:selected";
  128. break;
  129. case 'Staple_Form_RadioGroup':
  130. $identstring = "input:radio[name={$field->getName()}]:checked";
  131. break;
  132. case 'Staple_Form_CheckboxElement':
  133. return '';
  134. break;
  135. default:
  136. $identstring = $fieldid;
  137. }
  138. $script = "\t//Identical Validator for ".addslashes($field->getLabel())."\n";
  139. $script .= "\tif(!($('$valstring').val() == $('$identstring').val()))\n\t{\n";
  140. $script .= "\t\terrors.push('".addslashes($field->getLabel()).": \\n{$this->clientJSError()}\\n');\n";
  141. $script .= "\t\t$('$fieldid').addClass('form_error');\n";
  142. $script .= "\t}\n";
  143. $script .= "\telse {\n";
  144. $script .= "\t\t$('$fieldid').removeClass('form_error');\n";
  145. $script .= "\t}\n";
  146. return $script;
  147. }
  148. }
  149. ?>