Exemple #1
0
 public function validate()
 {
     $this->_errors = array();
     foreach ($this->columns as $column) {
         if ($column["validate"]) {
             $pattern = "#[a-z]+\\(([a-zA-Z0-9, ]+)\\)#";
             $raw = $column["raw"];
             $name = $column["name"];
             $validators = $column["validate"];
             $label = $column["label"];
             $defined = $this->getValidators();
             foreach ($validators as $validator) {
                 $function = $validator;
                 $arguments = array($this->{$raw});
                 $match = StringMethods::match($validator, $pattern);
                 if (count($match) > 0) {
                     $matches = StringMethods::split($match[0], ",\\s*");
                     $arguments = array_merge($arguments, $matches);
                     $offset = StringMethods::indexOf($validator, "(");
                     $function = substr($validator, 0, $offset);
                 }
                 if (!isset($defined[$function])) {
                     throw new Exception\Validation("The {$function} validator is not defined");
                 }
                 $template = $defined[$function];
                 if (!call_user_func_array(array($this, $template["handler"]), $arguments)) {
                     $replacements = array_merge(array($label ? $label : $raw), $arguments);
                     $message = $template["message"];
                     foreach ($replacements as $i => $replacement) {
                         $message = str_replace("{{$i}}", $replacement, $message);
                     }
                     if (!isset($this->_errors[$name])) {
                         $this->_errors[$name] = array();
                     }
                     $this->_errors[$name][] = $message;
                 }
             }
         }
     }
     return !sizeof($this->errors);
 }