Esempio n. 1
0
 function setValue($values)
 {
     if ($values) {
         $ds = new TableReader('blog_tag');
         $tags = $ds->select('tag')->where('blog_tag_id', $values, 'in')->orderby('tag')->fetchArray();
         $this->_value = implode($this->separator . ($this->separator != ' ' ? ' ' : ''), $tags);
     }
 }
 /**
  * Validates the passed value with respect to the options specified
  *
  * @param mixed $value The value that wants to be validated
  * @return bool True if the value is valid, false if not.
  */
 function validate($value)
 {
     if (is_array($value) || is_object($value)) {
         $this->error = 'array_type';
         return false;
     }
     $valid = $this->doGeneralChecks($value);
     if ($valid !== null) {
         return $valid;
     }
     $is_null = in_array($value, $this->options['null_values'], true);
     if ($this->options['required'] && $is_null) {
         $this->error = "required";
         return false;
     }
     if ($is_null) {
         return true;
     }
     if ($this->options['expression'] && !preg_match($this->options['expression'], $value)) {
         $this->error = 'expression';
         return false;
     }
     /**
      * WTF? in_array returns true if the array contains the number 0
      * and the value searched is any string other than a number.
      */
     if (!empty($this->options['in'])) {
         foreach ($this->options['in'] as &$in) {
             if ($in === 0) {
                 $in = '0';
             }
         }
     }
     if (!empty($this->options['in']) && !in_array($value, $this->options['in'])) {
         $this->error = 'in';
         return false;
     }
     if (!empty($this->options['not_in']) && in_array($value, $this->options['not_in'])) {
         $this->error = 'not_in';
         return false;
     }
     $str_value = (string) $value;
     //Assumes mbstring extension installed
     if ($this->options['max_length'] !== null && mb_strlen($str_value) > $this->options['max_length']) {
         $this->error = 'max_length';
         return false;
     }
     //Assumes mbstring extension installed
     if ($this->options['min_length'] !== null && mb_strlen($str_value) < $this->options['min_length']) {
         $this->error = 'min_length';
         return false;
     }
     if ($this->options['encoding']) {
         $converted = @iconv(AppConfig::CHARSET, AppConfig::CHARSET, $str_value);
         if (mb_strlen($converted) != mb_strlen($str_value)) {
             $this->error = 'encoding';
             return false;
         }
     }
     $float_value = (double) $value;
     if ($this->options['max_value'] !== null && $float_value > $this->options['max_value']) {
         $this->error = 'max_value';
         return false;
     }
     if ($this->options['min_value'] !== null && $float_value < $this->options['min_value']) {
         $this->error = 'min_value';
         return false;
     }
     if ($this->options['db_in_column']) {
         list($table_name, $column_name) = $this->options['db_in_column'];
         $query = new TableReader($table_name);
         $count = $query->select(array('count', '*'))->where($column_name, $value)->fetchScalar();
         if ($count == 0) {
             $this->error = 'db_in_column';
             return false;
         }
     }
     if ($this->options['db_not_in_column']) {
         list($table_name, $column_name) = $this->options['db_not_in_column'];
         $query = new TableReader($table_name);
         $count = $query->select(array('count', '*'))->where($column_name, $value)->fetchScalar();
         if ($count > 0) {
             $this->error = 'db_not_in_column';
             return false;
         }
     }
     return true;
 }