/** * Validates a text 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) { $return = parent::validate($value); if ($return !== true) { return $return; } $maxlength = get::array_def($this->attributes, 'maxlength', 0); $pattern = get::array_def($this->attributes, 'pattern', null); $msglbl = get::array_def($this->attributes, 'msglbl', get::array_def($this->attributes, 'name', $this->getId())); if ($maxlength > 0 && strlen(trim($value)) > $maxlength) { return sprintf('"%s" has to many characters - the max length is %s.', $msglbl, $maxlength); } if ($pattern != null && isset($value) && strlen(trim($value)) > 0 && !preg_match('/^(?:' . $pattern . ')$/', $value)) { return sprintf('"%s" does not match the pattern defined for it.', $msglbl); } return true; }
/** * Validates a color value. According to the W3C this field should ALWAYS have a 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) { $return = parent::validate($value); if ($return !== true) { return $return; } if (!isset($value) || strlen(trim($value)) < 1) { $value = '#000000'; } if (isset($value) && strlen(trim($value)) > 0) { $valid = is::color($value); if ($valid === false) { $msglbl = get::array_def($this->attributes, 'msglbl', get::array_def($this->attributes, 'name', $this->getId())); return sprintf('"%s" has an invalid color (ex. #FFFFFF).', $msglbl); } $value = $valid; } return true; }