/** * Adds a validation rule for the current control. * @param mixed rule type * @param string message to display for invalid data * @param mixed optional rule arguments * @return Rules provides a fluent interface */ public function addRule($operation, $message = NULL, $arg = NULL) { $rule = new Rule(); $rule->control = $this->control; $rule->operation = $operation === ':protection' ? ':equal' : $operation; $this->adjustOperation($rule); $rule->arg = $arg; $rule->type = Rule::VALIDATOR; if ($message === NULL && is_string($operation) && isset(self::$defaultMessages[$operation])) { $rule->message = self::$defaultMessages[$operation]; } else { $rule->message = $message; } if ($this->parent === NULL) { // notify only direct rules $this->control->notifyRule($rule); } $this->rules[] = $rule; return $this; }
/** * Filled validator: is control filled? * @param IFormControl * @return bool */ public static function validateFilled(IFormControl $control) { return $control->isFilled(); }
/** * Renders 'control' part of visual row of controls. * @param IFormControl * @return string */ public function renderControl(IFormControl $control) { $body = $this->getWrapper('control container'); if ($this->counter % 2) $body->class($this->getValue('control .odd'), TRUE); $description = $control->getOption('description'); if ($description instanceof Html) { $description = ' ' . $control->getOption('description'); } elseif (is_string($description)) { $description = ' ' . $this->getWrapper('control description')->setText($control->translate($description)); } else { $description = ''; } if ($control->getOption('required')) { $description = $this->getValue('control requiredsuffix') . $description; } if ($this->getValue('control errors')) { $description .= $this->renderErrors($control); } if ($control instanceof Checkbox || $control instanceof Button) { return $body->setHtml((string) $control->getControl() . (string) $control->getLabel() . $description); } else { return $body->setHtml((string) $control->getControl() . $description); } }
/** * Filled validator: has been any radio button selected? * @param IFormControl * @return bool */ public static function validateFilled(IFormControl $control) { return $control->getValue() !== NULL; }
/** * Filled validator: has been any file uploaded? * @param IFormControl * @return bool */ public static function validateFilled(IFormControl $control) { $file = $control->getValue(); return $file instanceof HttpUploadedFile && $file->isOK(); }
/** * Filled validator: has been any item selected? * @param IFormControl * @return bool */ public static function validateFilled(IFormControl $control) { $value = $control->getValue(); return is_array($value) ? count($value) > 0 : $value !== NULL; }
private function getClientScript(IFormControl $control, $operation, $arg) { $operation = strtolower($operation); $elem = 'form[' . Nette\Json::encode($control->getHtmlName()) . ']'; switch (TRUE) { case $control instanceof HiddenField || $control->isDisabled(): return NULL; case $operation === ':filled' && $control instanceof RadioList: return "res = (val = nette.getValue({$elem})) !== null;"; case $operation === ':submitted' && $control instanceof SubmitButton: return "res = sender && sender.name==" . Nette\Json::encode($control->getHtmlName()) . ";"; case $operation === ':equal' && $control instanceof MultiSelectBox: $tmp = array(); foreach (is_array($arg) ? $arg : array($arg) as $item) { $tmp[] = "options[i].value==" . Nette\Json::encode((string) $item); } $first = $control->isFirstSkipped() ? 1 : 0; return "var options = {$elem}.options; res = false;\n" . "for (var i={$first}, len=options.length; i<len; i++)\n\t" . "if (options[i].selected && (" . implode(' || ', $tmp) . ")) { res = true; break; }"; case $operation === ':filled' && $control instanceof SelectBox: return "res = {$elem}.selectedIndex >= " . ($control->isFirstSkipped() ? 1 : 0) . ";"; case $operation === ':filled' && $control instanceof TextBase: return "val = nette.getValue({$elem}); res = val!='' && val!=" . Nette\Json::encode((string) $control->getEmptyValue()) . ";"; case $operation === ':minlength' && $control instanceof TextBase: return "res = (val = nette.getValue({$elem})).length>=" . (int) $arg . ";"; case $operation === ':maxlength' && $control instanceof TextBase: return "res = (val = nette.getValue({$elem})).length<=" . (int) $arg . ";"; case $operation === ':length' && $control instanceof TextBase: if (!is_array($arg)) { $arg = array($arg, $arg); } return "val = nette.getValue({$elem}); res = " . ($arg[0] === NULL ? "true" : "val.length>=" . (int) $arg[0]) . " && " . ($arg[1] === NULL ? "true" : "val.length<=" . (int) $arg[1]) . ";"; case $operation === ':email' && $control instanceof TextBase: return 'res = /^[^@\\s]+@[^@\\s]+\\.[a-z]{2,10}$/i.test(val = nette.getValue(' . $elem . '));'; case $operation === ':url' && $control instanceof TextBase: return 'res = /^.+\\.[a-z]{2,6}(\\/.*)?$/i.test(val = nette.getValue(' . $elem . '));'; case $operation === ':regexp' && $control instanceof TextBase: if (!preg_match('#^(/.*/)([imu]*)$#', $arg, $matches)) { return NULL; // regular expression must be JavaScript compatible } $arg = $matches[1] . str_replace('u', '', $matches[2]); return "res = {$arg}.test(val = nette.getValue({$elem}));"; case $operation === ':integer' && $control instanceof TextBase: return "res = /^-?[0-9]+\$/.test(val = nette.getValue({$elem}));"; case $operation === ':float' && $control instanceof TextBase: return "res = /^-?[0-9]*[.,]?[0-9]+\$/.test(val = nette.getValue({$elem}));"; case $operation === ':range' && $control instanceof TextBase: return "val = nette.getValue({$elem}); res = " . ($arg[0] === NULL ? "true" : "parseFloat(val)>=" . Nette\Json::encode((double) $arg[0])) . " && " . ($arg[1] === NULL ? "true" : "parseFloat(val)<=" . Nette\Json::encode((double) $arg[1])) . ";"; case $operation === ':filled' && $control instanceof FormControl: return "res = (val = nette.getValue({$elem})) != '';"; case $operation === ':valid' && $control instanceof FormControl: return "res = !this[" . Nette\Json::encode($control->getHtmlName()) . "](sender);"; case $operation === ':equal' && $control instanceof FormControl: if ($control instanceof Checkbox) { $arg = (bool) $arg; } $tmp = array(); foreach (is_array($arg) ? $arg : array($arg) as $item) { if ($item instanceof IFormControl) { // compare with another form control? $tmp[] = "val==nette.getValue(form[" . Nette\Json::encode($item->getHtmlName()) . "])"; } else { $tmp[] = "val==" . Nette\Json::encode($item); } } return "val = nette.getValue({$elem}); res = (" . implode(' || ', $tmp) . ");"; } }
/** * Filled validator: is control filled? * @param IFormControl * @return bool */ public static function validateFilled(IFormControl $control) { return (string) $control->getValue() !== ''; // NULL, FALSE, '' ==> FALSE }
public static function validateFilled(IFormControl $control) { return $control->getValue() instanceof DateTime; }