/** * Validates against ruleset. * @return bool */ public function validate() { foreach ($this as $rule) { $success = $this->validateRule($rule); if ($success && $rule->branch && !$rule->branch->validate()) { return FALSE; } elseif (!$success && !$rule->branch) { $rule->control->addError(Validator::formatMessage($rule, TRUE)); return FALSE; } } return TRUE; }
/** * @return array */ public static function exportRules(Rules $rules) { $payload = array(); foreach ($rules as $rule) { if (!is_string($op = $rule->validator)) { if (!Nette\Utils\Callback::isStatic($op)) { continue; } $op = Nette\Utils\Callback::toString($op); } if ($rule->branch) { $item = array('op' => ($rule->isNegative ? '~' : '') . $op, 'rules' => static::exportRules($rule->branch), 'control' => $rule->control->getHtmlName()); if ($rule->branch->getToggles()) { $item['toggle'] = $rule->branch->getToggles(); } } else { $item = array('op' => ($rule->isNegative ? '~' : '') . $op, 'msg' => Validator::formatMessage($rule, FALSE)); } if (is_array($rule->arg)) { foreach ($rule->arg as $key => $value) { $item['arg'][$key] = $value instanceof IControl ? array('control' => $value->getHtmlName()) : $value; } } elseif ($rule->arg !== NULL) { $item['arg'] = $rule->arg instanceof IControl ? array('control' => $rule->arg->getHtmlName()) : $rule->arg; } $payload[] = $item; } return $payload; }
/** * Validates against ruleset. * @return bool */ public function validate($emptyOptional = FALSE) { $emptyOptional = $emptyOptional || $this->isOptional() && !$this->control->isFilled(); foreach ($this as $rule) { if (!$rule->branch && $emptyOptional && $rule->validator !== Form::FILLED) { continue; } $success = $this->validateRule($rule); if ($success && $rule->branch && !$rule->branch->validate($rule->validator === Form::BLANK ? FALSE : $emptyOptional)) { return FALSE; } elseif (!$success && !$rule->branch) { $rule->control->addError(Validator::formatMessage($rule, TRUE)); return FALSE; } } return TRUE; }