public static function validateValid(IFormControl $control)
 {
     $res = $control->getExtension()->validate($control->hash, $control->responce);
     if ($res) {
         return true;
     } else {
         if ($control->show_captcha_error) {
             foreach ($control->getExtension()->getErrors() as $error) {
                 $control->addError($error);
             }
         }
         $control->error = $control->getExtension()->getErrors();
         return false;
     }
 }
Example #2
0
 /**
  * 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;
     $this->adjustOperation($rule);
     $rule->arg = $arg;
     $rule->type = Rule::VALIDATOR;
     if ($message === NULL && isset(self::$defaultMessages[$rule->operation])) {
         $rule->message = self::$defaultMessages[$rule->operation];
     } else {
         $rule->message = $message;
     }
     if ($this->parent === NULL) {
         // notify only direct rules
         $this->control->notifyRule($rule);
     }
     $this->rules[] = $rule;
     return $this;
 }
 /**
  * Exports description for JavaScript.
  * @return array
  */
 private function exportControl(IFormControl $control)
 {
     return $control->isDisabled() ? NULL : array('class' => $control->getClass(), 'rules' => $this->exportRules($control->getRules()), 'opt' => $control instanceof FormControl ? $control->getOptions() : NULL, 'scope' => $control instanceof ISubmitterControl ? (bool) $control->getValidationScope() : NULL);
 }
Example #4
0
 /**
  * Filled validator: is control filled?
  * @param  IFormControl
  * @return bool
  */
 public static function validateFilled(IFormControl $control)
 {
     return (string) $control->getValue() !== '';
     // NULL, FALSE, '' ==> FALSE
 }
Example #5
0
 /**
  * Filled validator: has been any radio button selected?
  * @param  IFormControl
  * @return bool
  */
 public static function validateFilled(IFormControl $control)
 {
     return $control->getValue() !== NULL;
 }
Example #6
0
 /**
  * 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();
 }
Example #7
0
 private function getClientScript(IFormControl $control, $operation, $arg)
 {
     $operation = strtolower($operation);
     switch (TRUE) {
         case $control instanceof HiddenField || $control->isDisabled():
             return NULL;
         case $operation === ':filled' && $control instanceof RadioList:
             return $this->getValueScript($control) . "res = val !== null;";
         case $operation === ':submitted' && $control instanceof SubmitButton:
             return "element=null; res=sender && sender.name==" . json_encode($control->getHtmlName()) . ";";
         case $operation === ':equal' && $control instanceof MultiSelectBox:
             $tmp = array();
             foreach (is_array($arg) ? $arg : array($arg) as $item) {
                 $tmp[] = "element.options[i].value==" . json_encode((string) $item);
             }
             $first = $control->isFirstSkipped() ? 1 : 0;
             return "element = document.getElementById(" . json_encode($control->getHtmlId()) . ");\n\tres = false;\n\t\t" . "for (var i={$first};i<element.options.length;i++)\n\t\t\t" . "if (element.options[i].selected && (" . implode(' || ', $tmp) . ")) { res = true; break; }";
         case $operation === ':filled' && $control instanceof SelectBox:
             return "element = document.getElementById(" . json_encode($control->getHtmlId()) . ");\n\t\t" . "res = element.selectedIndex >= " . ($control->isFirstSkipped() ? 1 : 0) . ";";
         case $operation === ':filled' && $control instanceof TextInput:
             return $this->getValueScript($control) . "res = val!='' && val!=" . json_encode((string) $control->getEmptyValue()) . ";";
         case $operation === ':minlength' && $control instanceof TextBase:
             return $this->getValueScript($control) . "res = val.length>=" . (int) $arg . ";";
         case $operation === ':maxlength' && $control instanceof TextBase:
             return $this->getValueScript($control) . "res = val.length<=" . (int) $arg . ";";
         case $operation === ':length' && $control instanceof TextBase:
             if (!is_array($arg)) {
                 $arg = array($arg, $arg);
             }
             return $this->getValueScript($control) . "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 $this->getValueScript($control) . 'res = /^[^@]+@[^@]+\\.[a-z]{2,6}$/i.test(val);';
         case $operation === ':url' && $control instanceof TextBase:
             return $this->getValueScript($control) . 'res = /^.+\\.[a-z]{2,6}(\\/.*)?$/i.test(val);';
         case $operation === ':regexp' && $control instanceof TextBase:
             if (strncmp($arg, '/', 1)) {
                 throw new InvalidStateException("Regular expression '{$arg}' must be JavaScript compatible.");
             }
             return $this->getValueScript($control) . "res = {$arg}.test(val);";
         case $operation === ':integer' && $control instanceof TextBase:
             return $this->getValueScript($control) . "res = /^-?[0-9]+\$/.test(val);";
         case $operation === ':float' && $control instanceof TextBase:
             return $this->getValueScript($control) . "res = /^-?[0-9]*[.,]?[0-9]+\$/.test(val);";
         case $operation === ':range' && $control instanceof TextBase:
             return $this->getValueScript($control) . "res = " . ($arg[0] === NULL ? "true" : "parseFloat(val)>=" . json_encode((double) $arg[0])) . " && " . ($arg[1] === NULL ? "true" : "parseFloat(val)<=" . json_encode((double) $arg[1])) . ";";
         case $operation === ':filled' && $control instanceof FormControl:
             return $this->getValueScript($control) . "res = val!='';";
         case $operation === ':valid' && $control instanceof FormControl:
             return $this->getValueScript($control) . "res = function(){\n\t" . $this->getValidateScript($control->getRules(), TRUE) . "return true; }();";
         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==function(){var element;" . $this->getValueScript($item) . "return val;}()";
                 } else {
                     $tmp[] = "val==" . json_encode($item);
                 }
             }
             return $this->getValueScript($control) . "res = (" . implode(' || ', $tmp) . ");";
     }
 }
 /**
  * Adds the control identified by name.
  *
  * IF control is file then form enctype is changed to multipart.
  *
  * @return Form itself
  */
 function addControl(IFormControl $control)
 {
     $name = $control->getName();
     Assert::isFalse(isset($this->controls[$name]), 'control with name %s already exists', $name);
     $this->controls[$name] = $control;
     if ($control instanceof FileFormControl || $control instanceof FileFormControlSet) {
         $this->setEnctype(new FormEnctype(FormEnctype::MULTIPART));
     }
     return $this;
 }
Example #9
0
 /**
  * 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($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);
     }
 }
Example #10
0
 /**
  * Filled validator: is control filled?
  * @param  IFormControl
  * @return bool
  */
 public static function validateFilled(IFormControl $control)
 {
     return $control->isFilled();
 }
Example #11
0
 /**
  * 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;
 }
Example #12
0
 private function getClientScript(IFormControl $control, $operation, $arg)
 {
     $operation = strtolower($operation);
     $elem = 'form[' . json_encode($control->getHtmlName()) . ']';
     switch (TRUE) {
         case $control instanceof HiddenField || $control->isDisabled():
             return NULL;
         case $operation === ':filled' && $control instanceof RadioList:
             return "res = nette.getValue({$elem}) !== null;";
         case $operation === ':submitted' && $control instanceof SubmitButton:
             return "res=sender && sender.name==" . 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==" . 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 "var val = nette.getValue({$elem}); res = val!='' && val!=" . json_encode((string) $control->getEmptyValue()) . ";";
         case $operation === ':minlength' && $control instanceof TextBase:
             return "res = nette.getValue({$elem}).length>=" . (int) $arg . ";";
         case $operation === ':maxlength' && $control instanceof TextBase:
             return "res = nette.getValue({$elem}).length<=" . (int) $arg . ";";
         case $operation === ':length' && $control instanceof TextBase:
             if (!is_array($arg)) {
                 $arg = array($arg, $arg);
             }
             return "var 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(nette.getValue(' . $elem . '));';
         case $operation === ':url' && $control instanceof TextBase:
             return 'res = /^.+\\.[a-z]{2,6}(\\/.*)?$/i.test(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(nette.getValue({$elem}));";
         case $operation === ':integer' && $control instanceof TextBase:
             return "res = /^-?[0-9]+\$/.test(nette.getValue({$elem}));";
         case $operation === ':float' && $control instanceof TextBase:
             return "res = /^-?[0-9]*[.,]?[0-9]+\$/.test(nette.getValue({$elem}));";
         case $operation === ':range' && $control instanceof TextBase:
             return "var val = nette.getValue({$elem}); res = " . ($arg[0] === NULL ? "true" : "parseFloat(val)>=" . json_encode((double) $arg[0])) . " && " . ($arg[1] === NULL ? "true" : "parseFloat(val)<=" . json_encode((double) $arg[1])) . ";";
         case $operation === ':filled' && $control instanceof FormControl:
             return "res = nette.getValue({$elem}) != '';";
         case $operation === ':valid' && $control instanceof FormControl:
             return "res = !this[" . 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[" . json_encode($item->getHtmlName()) . "])";
                 } else {
                     $tmp[] = "val==" . json_encode($item);
                 }
             }
             return "var val = nette.getValue({$elem}); res = (" . implode(' || ', $tmp) . ");";
     }
 }
 private static function dumpControlError(IFormControl $control)
 {
     if (!$control->hasError()) {
         return '';
     }
     if ($control instanceof FormControlSet && $control->hasError(FormControlError::WRONG)) {
         $message = '<ul>';
         foreach ($control as $innerControl) {
             $message .= self::dumpControlError($innerControl);
         }
         $message .= '</ul>';
     } else {
         $message = $control->getErrorMessage();
     }
     return '<li>' . $control->getName() . (($label = $control->getLabel()) ? " ({$label})" : '') . ' is ' . $control->getError() . ': <i>' . $message . '</i></li>';
 }
Example #14
0
 /**
  * Adds a validation condition on specified 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, $arg = NULL)
 {
     $rule = new Rule();
     $rule->control = $control;
     $rule->operation = $operation;
     $this->adjustOperation($rule);
     $rule->arg = $arg;
     $rule->type = Rule::CONDITION;
     $rule->subRules = new self($this->control);
     $control->notifyRule($rule);
     $this->rules[] = $rule;
     return $rule->subRules;
 }
 private function getClientScript(IFormControl $control, $operation, $arg)
 {
     $id = $control->getHtmlId();
     $tmp = "el = document.getElementById('" . $id . "');\n\t";
     $tmp2 = "var val = el.value.replace(/^\\s+/, '').replace(/\\s+\$/, '');\n\t";
     $tmp3 = array();
     $operation = strtolower($operation);
     switch (TRUE) {
         case $control instanceof HiddenField || $control->isDisabled():
             return NULL;
         case $operation === ':equal' && $control instanceof Checkbox:
             return $tmp . "res = " . ($arg ? '' : '!') . "el.checked;";
         case $operation === ':filled' && $control instanceof FileUpload:
             return $tmp . "res = el.value!='';";
         case $operation === ':equal' && $control instanceof RadioList:
             foreach (is_array($arg) ? $arg : array($arg) as $item) {
                 $tmp3[] = "el.value==" . json_encode((string) $item);
             }
             return "res = false;\n\t" . "for (var i=0;i<" . count($control->getItems()) . ";i++) {\n\t\t" . "el = document.getElementById('" . $id . "-'+i);\n\t\t" . "if (el.checked && (" . implode(' || ', $tmp3) . ")) { res = true; break; }\n\t" . "}\n\tel = null;";
         case $operation === ':filled' && $control instanceof RadioList:
             return "res = false; el=null;\n\t" . "for (var i=0;i<" . count($control->getItems()) . ";i++) " . "if (document.getElementById('" . $id . "-'+i).checked) { res = true; break; }";
         case $operation === ':submitted' && $control instanceof SubmitButton:
             return "el=null; res=sender && sender.name==" . json_encode($control->getHtmlName()) . ";";
         case $operation === ':equal' && $control instanceof SelectBox:
             foreach (is_array($arg) ? $arg : array($arg) as $item) {
                 $tmp3[] = "el.options[i].value==" . json_encode((string) $item);
             }
             $first = $control->isFirstSkipped() ? 1 : 0;
             return $tmp . "res = false;\n\t" . "for (var i={$first};i<el.options.length;i++)\n\t\t" . "if (el.options[i].selected && (" . implode(' || ', $tmp3) . ")) { res = true; break; }";
         case $operation === ':filled' && $control instanceof SelectBox:
             $first = $control->isFirstSkipped() ? 1 : 0;
             return $tmp . "res = el.selectedIndex >= {$first};";
         case $operation === ':filled' && $control instanceof TextInput:
             return $tmp . $tmp2 . "res = val!='' && val!=" . json_encode((string) $control->getEmptyValue()) . ";";
         case $operation === ':minlength' && $control instanceof TextBase:
             return $tmp . $tmp2 . "res = val.length>=" . (int) $arg . ";";
         case $operation === ':maxlength' && $control instanceof TextBase:
             return $tmp . $tmp2 . "res = val.length<=" . (int) $arg . ";";
         case $operation === ':length' && $control instanceof TextBase:
             if (!is_array($arg)) {
                 $arg = array($arg, $arg);
             }
             return $tmp . $tmp2 . "res = val.length>=" . (int) $arg[0] . " && val.length<=" . (int) $arg[1] . ";";
         case $operation === ':email' && $control instanceof TextBase:
             return $tmp . $tmp2 . 'res = /^[^@]+@[^@]+\\.[a-z]{2,6}$/i.test(val);';
         case $operation === ':url' && $control instanceof TextBase:
             return $tmp . $tmp2 . 'res = /^.+\\.[a-z]{2,6}(\\/.*)?$/i.test(val);';
         case $operation === ':regexp' && $control instanceof TextBase:
             if (strncmp($arg, '/', 1)) {
                 throw new InvalidStateException("Regular expression '{$arg}' must be JavaScript compatible.");
             }
             return $tmp . $tmp2 . "res = {$arg}.test(val);";
         case $operation === ':integer' && $control instanceof TextBase:
             return $tmp . $tmp2 . "res = /^-?[0-9]+\$/.test(val);";
         case $operation === ':float' && $control instanceof TextBase:
             return $tmp . $tmp2 . "res = /^-?[0-9]*[.,]?[0-9]+\$/.test(val);";
         case $operation === ':range' && $control instanceof TextBase:
             return $tmp . $tmp2 . "res = parseFloat(val)>=" . json_encode((double) $arg[0]) . " && parseFloat(val)<=" . json_encode((double) $arg[1]) . ";";
         case $operation === ':filled' && $control instanceof FormControl:
             return $tmp . $tmp2 . "res = val!='';";
         case $operation === ':valid' && $control instanceof FormControl:
             return $tmp . $tmp2 . "res = function(){\n\t" . $this->getValidateScript($control->getRules(), TRUE) . "return true; }();";
         case $operation === ':equal' && $control instanceof FormControl:
             foreach (is_array($arg) ? $arg : array($arg) as $item) {
                 if (is_object($item)) {
                     // compare with another form control?
                     $tmp3[] = get_class($item) === $control->getClass() ? "val==document.getElementById('" . $item->getHtmlId() . "').value" : 'false';
                 } else {
                     $tmp3[] = "val==" . json_encode((string) $item);
                 }
             }
             return $tmp . $tmp2 . "res = (" . implode(' || ', $tmp3) . ");";
     }
 }