/** * @return array */ public static function exportRules(Rules $rules) { $payload = []; 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 = ['op' => ($rule->isNegative ? '~' : '') . $op, 'rules' => static::exportRules($rule->branch), 'control' => $rule->control->getHtmlName()]; if ($rule->branch->getToggles()) { $item['toggle'] = $rule->branch->getToggles(); } elseif (!$item['rules']) { continue; } } else { $item = ['op' => ($rule->isNegative ? '~' : '') . $op, 'msg' => Validator::formatMessage($rule, FALSE)]; } if (is_array($rule->arg)) { $item['arg'] = []; foreach ($rule->arg as $key => $value) { $item['arg'][$key] = $value instanceof IControl ? ['control' => $value->getHtmlName()] : $value; } } elseif ($rule->arg !== NULL) { $item['arg'] = $rule->arg instanceof IControl ? ['control' => $rule->arg->getHtmlName()] : $rule->arg; } $payload[] = $item; } if ($payload && $rules->isOptional()) { array_unshift($payload, ['op' => 'optional']); } return $payload; }
/** * Performs the server side validation. * @return void */ public function validate() { if ($this->isDisabled()) { return; } $this->cleanErrors(); $this->rules->validate(); }
/** * Adds a validation condition based on another control a returns new branch. * @param IFormControl form control * @param mixed condition type * @param mixed optional condition arguments * @return Rules new branch */ public function addConditionOn(IFormControl $control, $operation, $value = NULL) { return $this->rules->addConditionOn($control, $operation, $value); }
private function getToggleScript(Rules $rules, $cond = NULL) { $s = ''; foreach ($rules->getToggles() as $id => $visible) { $s .= "visible = true; {$cond}\n" . "nette.toggle(" . Nette\Json::encode((string) $id) . ", " . ($visible ? '' : '!') . "visible);\n"; } $formName = Nette\Json::encode((string) $this->form->getElementPrototype()->id); foreach ($rules as $rule) { if ($rule->type === Rule::CONDITION && is_string($rule->operation)) { $script = $this->getClientScript($rule->control, $rule->operation, $rule->arg); if ($script) { $res = $this->getToggleScript($rule->subRules, $cond . "{$script} visible = visible && " . ($rule->isNegative ? '!' : '') . "res;\n"); if ($res) { $el = $rule->control->getControlPrototype(); if ($el->getName() === 'select') { $el->onchange("nette.forms[{$formName}].toggle(this)", TRUE); } else { $el->onclick("nette.forms[{$formName}].toggle(this)", TRUE); //$el->onkeyup("nette.forms[$formName].toggle(this)", TRUE); } $s .= $res; } } } } return $s; }
/** * Makes control mandatory. * @param string error message * @return FormControl provides a fluent interface * @deprecated */ public final function setRequired($message = NULL) { $this->rules->addRule(':Filled', $message); return $this; }
/** * Makes control mandatory. * @param string error message * @return FormControl provides a fluent interface * @deprecated */ public final function setRequired($message = NULL) { $this->rules->addRule(Form::FILLED, $message); return $this; }
/** * Adds a validation rule. * * @param mixed rule type * @param string message to display for invalid data * @param mixed optional rule arguments * * @return BaseControl provides a fluent interface */ public function addRule($operation, $message = null, $arg = null) { $this->rules->addRule($operation, $message, $arg); return $this; }