Validator.class.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. abstract class Staple_Form_Validator
  3. {
  4. /**
  5. * This class constant defines the default error message for the validator. Each child class should customize this value.
  6. * @var string
  7. */
  8. const DEFAULT_ERROR = 'An error occurred while validating the field.';
  9. /**
  10. * An array that holds the default error messages for the validator.
  11. * @var array
  12. */
  13. protected $error = array();
  14. /**
  15. * A string that holds the user-defined error message when validation fails.
  16. * @var string
  17. */
  18. protected $usermsg;
  19. /**
  20. * Default Constructor. Supports and optional user defined error message.
  21. * @param string $usermsg
  22. */
  23. public function __construct($usermsg = NULL)
  24. {
  25. if(isset($usermsg))
  26. {
  27. $this->setUserErrorMessage($usermsg);
  28. }
  29. }
  30. /**
  31. * Factory function to create objects.
  32. * @param string $usermsg
  33. */
  34. public static function Create($usermsg = NULL)
  35. {
  36. return new static($usermsg);
  37. }
  38. /*
  39. public function getError()
  40. {
  41. return $this->error;
  42. }*/
  43. /**
  44. * Gets the error message for client side checking.
  45. */
  46. public function clientJSError()
  47. {
  48. if(isset($this->usermsg))
  49. {
  50. return $this->usermsg;
  51. }
  52. else
  53. {
  54. return static::DEFAULT_ERROR;
  55. }
  56. }
  57. /**
  58. * Clears all the errors in the errors array.
  59. */
  60. public function clearErrors()
  61. {
  62. $this->error = array();
  63. return $this;
  64. }
  65. /**
  66. * Adds a custom error or adds the default error to the errors array.
  67. * @param string $err
  68. */
  69. public function addError($err = NULL)
  70. {
  71. if(isset($err))
  72. {
  73. $this->error[] = $err;
  74. }
  75. elseif(isset($this->usermsg))
  76. {
  77. $this->error[] = $this->usermsg;
  78. }
  79. else
  80. {
  81. $this->error[] = static::DEFAULT_ERROR;
  82. }
  83. return $this;
  84. }
  85. /**
  86. * @return the $usermsg
  87. */
  88. public function getErrorsAsString()
  89. {
  90. $eString = implode("\n", $this->error);
  91. if(isset($this->usermsg))
  92. {
  93. $eString = $this->usermsg."\n".$eString;
  94. }
  95. return $eString;
  96. }
  97. /**
  98. * @param string $usermsg
  99. */
  100. public function setUserErrorMessage($usermsg)
  101. {
  102. $this->usermsg = $usermsg;
  103. }
  104. /**
  105. * Return the errors array
  106. * @return array
  107. */
  108. public function getErrors()
  109. {
  110. return $this->error;
  111. }
  112. /**
  113. * Returns the name of the validator
  114. * @return string
  115. */
  116. public function getName()
  117. {
  118. $c = get_class($this);
  119. $c = str_replace('Staple_Form_Validate_', '', $c);
  120. return $c;
  121. }
  122. /**
  123. * Function for client side form checking. Must be overridden in the child class.
  124. * @param string $fieldType
  125. * @param Staple_Form_Element $field
  126. */
  127. public function clientJS($fieldType, Staple_Form_Element $field)
  128. {
  129. return '';
  130. }
  131. /**
  132. * Function for client side form checking. Must be overridden in the child class. This one is specific to JQuery.
  133. * @param string $fieldType
  134. * @param Staple_Form_Element $field
  135. */
  136. public function clientJQuery($fieldType, Staple_Form_Element $field)
  137. {
  138. return '';
  139. }
  140. /**
  141. *
  142. * Returns a boolean true or false on success or failure of the validation check.
  143. * @param mixed $data
  144. * @return bool
  145. */
  146. abstract public function check($data);
  147. }