123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- <?php
- abstract class Staple_Form_Validator
- {
- /**
- * This class constant defines the default error message for the validator. Each child class should customize this value.
- * @var string
- */
- const DEFAULT_ERROR = 'An error occurred while validating the field.';
- /**
- * An array that holds the default error messages for the validator.
- * @var array
- */
- protected $error = array();
- /**
- * A string that holds the user-defined error message when validation fails.
- * @var string
- */
- protected $usermsg;
-
- /**
- * Default Constructor. Supports and optional user defined error message.
- * @param string $usermsg
- */
- public function __construct($usermsg = NULL)
- {
- if(isset($usermsg))
- {
- $this->setUserErrorMessage($usermsg);
- }
- }
-
- /**
- * Factory function to create objects.
- * @param string $usermsg
- */
- public static function Create($usermsg = NULL)
- {
- return new static($usermsg);
- }
-
- /*
- public function getError()
- {
- return $this->error;
- }*/
-
- /**
- * Gets the error message for client side checking.
- */
- public function clientJSError()
- {
- if(isset($this->usermsg))
- {
- return $this->usermsg;
- }
- else
- {
- return static::DEFAULT_ERROR;
- }
- }
-
- /**
- * Clears all the errors in the errors array.
- */
- public function clearErrors()
- {
- $this->error = array();
- return $this;
- }
-
- /**
- * Adds a custom error or adds the default error to the errors array.
- * @param string $err
- */
- public function addError($err = NULL)
- {
- if(isset($err))
- {
- $this->error[] = $err;
- }
- elseif(isset($this->usermsg))
- {
- $this->error[] = $this->usermsg;
- }
- else
- {
- $this->error[] = static::DEFAULT_ERROR;
- }
- return $this;
- }
- /**
- * @return the $usermsg
- */
- public function getErrorsAsString()
- {
- $eString = implode("\n", $this->error);
- if(isset($this->usermsg))
- {
- $eString = $this->usermsg."\n".$eString;
- }
- return $eString;
- }
- /**
- * @param string $usermsg
- */
- public function setUserErrorMessage($usermsg)
- {
- $this->usermsg = $usermsg;
- }
- /**
- * Return the errors array
- * @return array
- */
- public function getErrors()
- {
- return $this->error;
- }
-
- /**
- * Returns the name of the validator
- * @return string
- */
- public function getName()
- {
- $c = get_class($this);
- $c = str_replace('Staple_Form_Validate_', '', $c);
- return $c;
- }
-
- /**
- * Function for client side form checking. Must be overridden in the child class.
- * @param string $fieldType
- * @param Staple_Form_Element $field
- */
- public function clientJS($fieldType, Staple_Form_Element $field)
- {
- return '';
- }
-
- /**
- * Function for client side form checking. Must be overridden in the child class. This one is specific to JQuery.
- * @param string $fieldType
- * @param Staple_Form_Element $field
- */
- public function clientJQuery($fieldType, Staple_Form_Element $field)
- {
- return '';
- }
-
- /**
- *
- * Returns a boolean true or false on success or failure of the validation check.
- * @param mixed $data
- * @return bool
- */
- abstract public function check($data);
- }
|