/** * 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); }
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) . ");"; } }