Example #1
0
 /**
  * Validates a number value.
  * 
  * @param string $value The value to validate.
  * 
  * @return bool|string Returns boolean TRUE upon successfull validation, and an error message string upon failure.
  */
 public function validate(&$value)
 {
     $required = get::array_def($this->attributes, 'required', false) == 'required';
     $integeronly = get::array_def($this->attributes, 'integeronly', false, array(true, false));
     $msglbl = get::array_def($this->attributes, 'msglbl', get::array_def($this->attributes, 'name', $this->getId()));
     if ($required && (!isset($value) || strlen(trim($value)) < 1)) {
         return sprintf('"%s" is a required field.', $msglbl);
     }
     if (isset($value) && strlen(trim($value)) > 0) {
         $value = trim($value);
         $valid = $integeronly ? preg_match('/^[+-]?\\d+$/', $value) ? (int) $value : false : is::float($value);
         $errorText = $integeronly ? 'integer' : 'number';
         $min = get::array_def($this->attributes, 'min', null);
         $max = get::array_def($this->attributes, 'max', null);
         if ($valid === false) {
             return sprintf('"%s" is not a valid %s.', $msglbl, $errorText);
         } elseif (isset($min) && $valid < $min) {
             return sprintf('"%s" is not allowed to be less than %s.', $msglbl, $min);
         } elseif (isset($max) && $valid > $max) {
             return sprintf('"%s" is not allowed to be greater than %s.', $msglbl, $max);
         }
         $value = $valid;
     }
     return true;
 }