Exemple #1
0
 /**
  * Validates data against the set validation rules
  *
  * Returns boolean true when successful validate without errors.
  *
  * @param array $skip
  *            Optional array of fieldnames to skip on validation
  *
  * @return boolean
  */
 protected function validate(array &$data, array $fields = [], bool $filter_before_validate = true, array $skip = [])
 {
     static $validator;
     if (empty($fields)) {
         // No and no fields in scheme means no validation rules so we have nothing to du here
         if (empty($this->scheme['fields'])) {
             return $data;
         }
         // Use the fields list from existing scheme
         $fields = $this->scheme['fields'];
     }
     // Still no fields?
     if (empty($fields)) {
         return $data;
     }
     // Lets run our rules
     foreach ($data as $key => $val) {
         // Is this field inside the $skip array list?
         if (in_array($key, $skip)) {
             continue;
         }
         // Run possible filters before the validation process
         if ($filter_before_validate && !empty($fields[$key]['filter'])) {
             if (!is_array($fields[$key]['filter'])) {
                 $fields[$key]['filter'] = (array) $fields[$key]['filter'];
             }
             foreach ($fields[$key]['filter'] as $filter) {
                 $options = [];
                 if (is_array($filter)) {
                     $options = $filter[1];
                     $filter = $filter[0];
                 }
                 $result = filter_var($val, $filter, $options);
                 if ($result === false) {
                     $this->addError($key, sprintf($this->app->language->get('validator.filter'), $filter));
                 } else {
                     $data[$key] = $result;
                 }
             }
         }
         if (empty($fields[$key]['validate'])) {
             continue;
         }
         if (empty($validator)) {
             $validator = new Validator();
         }
         if (!is_array($fields[$key]['validate'])) {
             $fields[$key]['validate'] = (array) $fields[$key]['validate'];
         }
         $validator->setValue($val);
         foreach ($fields[$key]['validate'] as $rule) {
             $validator->parseRule($rule);
             $validator->validate();
             if (!$validator->isValid()) {
                 $result = $validator->getResult();
                 if (is_array($result)) {
                     $result = vsprintf($this->app->language->get($result[0]), $result[1]);
                 } else {
                     $result = $this->app->language->get($result);
                 }
                 $this->errors[$key][] = $result;
             }
         }
     }
     return $data;
 }
 /**
  * Creates a rule object.
  *
  * Ideal for rules where you need to combine or accees another rule.
  *
  * @param string $rule_name
  *
  * @return \Validator\Rules\RuleInterface
  */
 protected function createRule($rule_name) : RuleInterface
 {
     return $this->validator->createRule($rule_name);
 }