Exemplo n.º 1
0
 /**
  * Run the validation
  *
  * @param Array $values Data to validate
  * @param Bool $partial Partial validation? (@see `validatePartial()`)
  * @return Result object
  */
 protected function _validate($values, $partial = FALSE)
 {
     $result = new Result();
     if (is_object($values)) {
         $values = $this->prepForObject($values);
     }
     foreach ($this->rules as $key => $rules) {
         $value = NULL;
         if (array_key_exists($key, $values)) {
             $value = $values[$key];
         }
         $rules = $this->setupRules($rules);
         foreach ($rules as $rule) {
             if ($partial && $rule instanceof Rule\Required && $value === NULL) {
                 continue;
             }
             $rule->setAllValues($values);
             $rule_return = $rule->validate($key, $value);
             // Skip the rest of the rules?
             // e.g. Presence failed
             if ($rule->isFailed()) {
                 break;
             }
             // Hard stopping rule? Record the error and move on.
             // e.g. Required failed
             if ($rule->isStopped()) {
                 $result->addFailed($key, $rule);
                 break;
             }
             // Passed? Move on to the next rule
             if ($rule_return === TRUE) {
                 continue;
             }
             // At this point:
             //
             // 1) The rule failed (`$rule_return` === FALSE)
             // 2) This field is *not* required (no self::STOP)
             //
             // This means we have an incorrect optional value. Accordingly,
             // empty values are ok (because optional) anything else is not.
             if (is_string($value) && trim($value) !== '' || is_array($value) && !empty($value)) {
                 $result->addFailed($key, $rule);
             }
         }
     }
     return $result;
 }
Exemplo n.º 2
0
 public function update()
 {
     if (empty($_POST)) {
         ee()->functions->redirect($this->base_url);
     }
     $errors = new ValidationResult();
     $templates = ee()->api->get('Template')->with('TemplateGroup')->with('TemplateRoute')->filter('site_id', ee()->config->item('site_id'))->order('TemplateRoute.order', 'asc')->order('TemplateGroup.group_name', 'asc')->order('template_name', 'asc')->all();
     $submitted = ee()->input->post('routes');
     $order = array_keys($submitted);
     foreach ($templates as $template) {
         $id = $template->template_id;
         $submitted[$id]['route'] = trim($submitted[$id]['route']);
         if (!$template->TemplateRoute) {
             $template->TemplateRoute = ee('Model')->make('TemplateRoute');
         }
         $route = $template->TemplateRoute;
         if (empty($submitted[$id]['route'])) {
             $template->TemplateRoute = NULL;
             continue;
         }
         $route->route_required = $submitted[$id]['required'] != 'n';
         $route->route = $submitted[$id]['route'];
         $route->order = array_search($id, $order);
         $result = $route->validate();
         if ($result->isNotValid()) {
             foreach ($result->getFailed() as $field => $rules) {
                 foreach ($rules as $rule) {
                     $errors->addFailed("routes[{$id}][route]", $rule);
                 }
             }
         }
     }
     if ($errors->isValid()) {
         $templates->save();
         ee('CP/Alert')->makeInline()->asSuccess()->withTitle(lang('template_routes_saved'))->addToBody(lang('template_routes_saved_desc'))->defer();
         ee()->functions->redirect($this->base_url);
     } else {
         ee()->load->helper('html_helper');
         ee('CP/Alert')->makeInline()->asIssue()->withTitle(lang('template_routes_not_saved'))->addToBody(lang('template_routes_not_saved_desc'))->now();
         $this->index($templates, $errors);
     }
 }