Source for file InstantClientScript.php

Documentation is available at InstantClientScript.php

  1. 1: <?php
  2. 2:  
  3. 3: /**
  4. 4:  * Nette Framework
  5. 5:  *
  6. 6:  * Copyright (c) 2004, 2009 David Grudl (http://davidgrudl.com)
  7. 7:  *
  8. 8:  * This source file is subject to the "Nette license" that is bundled
  9. 9:  * with this package in the file license.txt.
  10. 10:  *
  11. 11:  * For more information please see https://nette.org
  12. 12:  *
  13. 13:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  14. 14:  * @license    https://nette.org/license  Nette license
  15. 15:  * @link       https://nette.org
  16. 16:  * @category   Nette
  17. 17:  * @package    Nette\Forms
  18. 18:  * @version    $Id$
  19. 19:  */
  20. 20:  
  21. 21:  
  22. 22:  
  23. 23: require_once dirname(__FILE__'/../../Object.php';
  24. 24:  
  25. 25:  
  26. 26:  
  27. 27: /**
  28. 28:  * Instant validation JavaScript generator.
  29. 29:  *
  30. 30:  * @author     David Grudl
  31. 31:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  32. 32:  * @package    Nette\Forms
  33. 33:  */
  34. 34: final class InstantClientScript extends Object
  35. 35: {
  36. 36:     /** @var string  JavaScript event handler name */
  37. 37:     public $validateFunction;
  38. 38:  
  39. 39:     /** @var string  JavaScript event handler name */
  40. 40:     public $toggleFunction;
  41. 41:  
  42. 42:     /** @var string  JavaScript code */
  43. 43:     public $doAlert = 'if (element) element.focus(); alert(message);';
  44. 44:  
  45. 45:     /** @var string  JavaScript code */
  46. 46:     public $doToggle = 'if (element) element.style.display = visible ? "" : "none";';
  47. 47:  
  48. 48:     /** @var string */
  49. 49:     public $validateScript;
  50. 50:  
  51. 51:     /** @var string */
  52. 52:     public $toggleScript;
  53. 53:  
  54. 54:     /** @var bool */
  55. 55:     private $central;
  56. 56:  
  57. 57:     /** @var Form */
  58. 58:     private $form;
  59. 59:  
  60. 60:  
  61. 61:  
  62. 62:     public function __construct(Form $form)
  63. 63:     {
  64. 64:         $this->form $form;
  65. 65:         $name ucfirst($form->getName())//ucfirst(strtr($form->getUniqueId(), Form::NAME_SEPARATOR, '_'));
  66. 66:         $this->validateFunction = 'validate' $name;
  67. 67:         $this->toggleFunction = 'toggle' $name;
  68. 68:     }
  69. 69:  
  70. 70:  
  71. 71:  
  72. 72:     public function enable()
  73. 73:     {
  74. 74:         $this->validateScript = '';
  75. 75:         $this->toggleScript = '';
  76. 76:         $this->central TRUE;
  77. 77:  
  78. 78:         foreach ($this->form->getControls(as $control{
  79. 79:             $script $this->getValidateScript($control->getRules());
  80. 80:             if ($script{
  81. 81:                 $this->validateScript .= "do {\n\t$script} while(0);\n\n\t";
  82. 82:             }
  83. 83:             $this->toggleScript .= $this->getToggleScript($control->getRules());
  84. 84:  
  85. 85:             if ($control instanceof ISubmitterControl && $control->getValidationScope(!== TRUE{
  86. 86:                 $this->central FALSE;
  87. 87:             }
  88. 88:         }
  89. 89:  
  90. 90:         if ($this->validateScript || $this->toggleScript{
  91. 91:             if ($this->central{
  92. 92:                 $this->form->getElementPrototype()->onsubmit("return $this->validateFunction(this)"TRUE);
  93. 93:  
  94. 94:             else {
  95. 95:                 foreach ($this->form->getComponents(TRUE'Nette\Forms\ISubmitterControl'as $control{
  96. 96:                     if ($control->getValidationScope()) {
  97. 97:                         $control->getControlPrototype()->onclick("return $this->validateFunction(this)"TRUE);
  98. 98:                     }
  99. 99:                 }
  100. 100:             }
  101. 101:         }
  102. 102:     }
  103. 103:  
  104. 104:  
  105. 105:  
  106. 106:     /**
  107. 107:      * Generates the client side validation script.
  108. 108:      * @return string 
  109. 109:      */
  110. 110:     public function renderClientScript()
  111. 111:     {
  112. 112:         $s '';
  113. 113:  
  114. 114:         if ($this->validateScript{
  115. 115:             $s .= "function $this->validateFunction(sender) {\n\t"
  116. 116:             . "var element, message, res;\n\t"
  117. 117:             . $this->validateScript
  118. 118:             . "return true;\n"
  119. 119:             . "}\n\n";
  120. 120:         }
  121. 121:  
  122. 122:         if ($this->toggleScript{
  123. 123:             $s .= "function $this->toggleFunction(sender) {\n\t"
  124. 124:             . "var element, visible, res;\n\t"
  125. 125:             . $this->toggleScript
  126. 126:             . "\n}\n\n"
  127. 127:             . "$this->toggleFunction(null);\n";
  128. 128:         }
  129. 129:  
  130. 130:         if ($s{
  131. 131:             return "<script type=\"text/javascript\">\n"
  132. 132:             . "/* <![CDATA[ */\n"
  133. 133:             . $s
  134. 134:             . "/* ]]> */\n"
  135. 135:             . "</script>";
  136. 136:         }
  137. 137:     }
  138. 138:  
  139. 139:  
  140. 140:  
  141. 141:     private function getValidateScript(Rules $rules$onlyCheck FALSE)
  142. 142:     {
  143. 143:         $res '';
  144. 144:         foreach ($rules as $rule{
  145. 145:             if (!is_string($rule->operation)) continue;
  146. 146:  
  147. 147:             if (strcasecmp($rule->operation'Nette\Forms\InstantClientScript::javascript'=== 0{
  148. 148:                 $res .= "$rule->arg\n\t";
  149. 149:                 continue;
  150. 150:             }
  151. 151:  
  152. 152:             $script $this->getClientScript($rule->control$rule->operation$rule->arg);
  153. 153:             if (!$scriptcontinue;
  154. 154:  
  155. 155:             if (!empty($rule->message)) // this is rule
  156. 156:                 if ($onlyCheck{
  157. 157:                     $res .= "$script\n\tif (($rule->isNegative '' '!'"res) { return false; }\n\t";
  158. 158:  
  159. 159:                 else {
  160. 160:                     $res .= "$script\n\t"
  161. 161:                         . "if (" ($rule->isNegative '' '!'"res) { "
  162. 162:                         . "message = " json_encode((string) vsprintf($rule->control->translate($rule->message)(array) $rule->arg)) "; "
  163. 163:                         . $this->doAlert
  164. 164:                         . " return false; }\n\t";
  165. 165:                 }
  166. 166:             }
  167. 167:  
  168. 168:             if ($rule->type === Rule::CONDITION// this is condition
  169. 169:                 $innerScript $this->getValidateScript($rule->subRules$onlyCheck);
  170. 170:                 if ($innerScript{
  171. 171:                     $res .= "$script\n\tif (($rule->isNegative '!' ''"res) {\n\t\t" str_replace("\n\t""\n\t\t"rtrim($innerScript)) "\n\t}\n\t";
  172. 172:                     if (!$onlyCheck && $rule->control instanceof ISubmitterControl{
  173. 173:                         $this->central FALSE;
  174. 174:                     }
  175. 175:                 }
  176. 176:             }
  177. 177:         }
  178. 178:         return $res;
  179. 179:     }
  180. 180:  
  181. 181:  
  182. 182:  
  183. 183:     private function getToggleScript(Rules $rules$cond NULL)
  184. 184:     {
  185. 185:         $s '';
  186. 186:         foreach ($rules->getToggles(as $id => $visible{
  187. 187:             $s .= "visible = true; {$cond}element = document.getElementById('$id "');\n\t"
  188. 188:                 . ($visible '' 'visible = !visible; ')
  189. 189:                 . $this->doToggle
  190. 190:                 . "\n\t";
  191. 191:         }
  192. 192:         foreach ($rules as $rule{
  193. 193:             if ($rule->type === Rule::CONDITION && is_string($rule->operation)) {
  194. 194:                 $script $this->getClientScript($rule->control$rule->operation$rule->arg);
  195. 195:                 if ($script{
  196. 196:                     $res $this->getToggleScript($rule->subRules$cond "$script visible = visible && ($rule->isNegative '!' ''"res;\n\t");
  197. 197:                     if ($res{
  198. 198:                         $el $rule->control->getControlPrototype();
  199. 199:                         if ($el->getName(=== 'select'{
  200. 200:                             $el->onchange("$this->toggleFunction(this)"TRUE);
  201. 201:                         else {
  202. 202:                             $el->onclick("$this->toggleFunction(this)"TRUE);
  203. 203:                             //$el->onkeyup("$this->toggleFunction(this)", TRUE);
  204. 204:                         }
  205. 205:                         $s .= $res;
  206. 206:                     }
  207. 207:                 }
  208. 208:             }
  209. 209:         }
  210. 210:         return $s;
  211. 211:     }
  212. 212:  
  213. 213:  
  214. 214:  
  215. 215:     private function getValueScript(IFormControl $control)
  216. 216:     {
  217. 217:         $tmp "element = document.getElementById(" json_encode($control->getHtmlId()) ");\n\t";
  218. 218:         switch (TRUE{
  219. 219:         case $control instanceof Checkbox:
  220. 220:             return $tmp "var val = element.checked;\n\t";
  221. 221:  
  222. 222:         case $control instanceof RadioList:
  223. 223:             return "for (var val=null, i=0; i<" count($control->getItems()) "; i++) {\n\t\t"
  224. 224:             . "element = document.getElementById(" json_encode($control->getHtmlId('-'"+i);\n\t\t"
  225. 225:             . "if (element.checked) { val = element.value; break; }\n\t"
  226. 226:             . "}\n\t";
  227. 227:  
  228. 228:         default:
  229. 229:             return $tmp "var val = element.value.replace(/^\\s+|\\s+\$/g, '');\n\t";
  230. 230:         }
  231. 231:     }
  232. 232:  
  233. 233:  
  234. 234:  
  235. 235:     private function getClientScript(IFormControl $control$operation$arg)
  236. 236:     {
  237. 237:         $operation strtolower($operation);
  238. 238:         switch (TRUE{
  239. 239:         case $control instanceof HiddenField || $control->isDisabled():
  240. 240:             return NULL;
  241. 241:  
  242. 242:         case $operation === ':filled' && $control instanceof RadioList:
  243. 243:             return $this->getValueScript($control"res = val !== null;";
  244. 244:  
  245. 245:         case $operation === ':submitted' && $control instanceof SubmitButton:
  246. 246:             return "element=null; res=sender && sender.name==" json_encode($control->getHtmlName()) ";";
  247. 247:  
  248. 248:         case $operation === ':equal' && $control instanceof MultiSelectBox:
  249. 249:             $tmp array();
  250. 250:             foreach ((is_array($arg$arg array($arg)) as $item{
  251. 251:                 $tmp["element.options[i].value==" json_encode((string) $item);
  252. 252:             }
  253. 253:             $first $control->isFirstSkipped(0;
  254. 254:             return "element = document.getElementById(" json_encode($control->getHtmlId()) ");\n\tres = false;\n\t"
  255. 255:                 . "for (var i=$first;i<element.options.length;i++)\n\t\t"
  256. 256:                 . "if (element.options[i].selected && (" implode(' || '$tmp")) { res = true; break; }";
  257. 257:  
  258. 258:         case $operation === ':filled' && $control instanceof SelectBox:
  259. 259:             return "element = document.getElementById(" json_encode($control->getHtmlId()) ");\n\t"
  260. 260:                 . "res = element.selectedIndex >= " ($control->isFirstSkipped(0";";
  261. 261:  
  262. 262:         case $operation === ':filled' && $control instanceof TextInput:
  263. 263:             return $this->getValueScript($control"res = val!='' && val!=" json_encode((string) $control->getEmptyValue()) ";";
  264. 264:  
  265. 265:         case $operation === ':minlength' && $control instanceof TextBase:
  266. 266:             return $this->getValueScript($control"res = val.length>=" . (int) $arg ";";
  267. 267:  
  268. 268:         case $operation === ':maxlength' && $control instanceof TextBase:
  269. 269:             return $this->getValueScript($control"res = val.length<=" . (int) $arg ";";
  270. 270:  
  271. 271:         case $operation === ':length' && $control instanceof TextBase:
  272. 272:             if (!is_array($arg)) {
  273. 273:                 $arg array($arg$arg);
  274. 274:             }
  275. 275:             return $this->getValueScript($control"res = " ($arg[0=== NULL "true" "val.length>=" . (int) $arg[0]" && "
  276. 276:                 . ($arg[1=== NULL "true" "val.length<=" . (int) $arg[1]";";
  277. 277:  
  278. 278:         case $operation === ':email' && $control instanceof TextBase:
  279. 279:             return $this->getValueScript($control'res = /^[^@]+@[^@]+\.[a-z]{2,6}$/i.test(val);';
  280. 280:  
  281. 281:         case $operation === ':url' && $control instanceof TextBase:
  282. 282:             return $this->getValueScript($control'res = /^.+\.[a-z]{2,6}(\\/.*)?$/i.test(val);';
  283. 283:  
  284. 284:         case $operation === ':regexp' && $control instanceof TextBase:
  285. 285:             if (strncmp($arg'/'1)) {
  286. 286:                 throw new InvalidStateException("Regular expression '$arg' must be JavaScript compatible.");
  287. 287:             }
  288. 288:             return $this->getValueScript($control"res = $arg.test(val);";
  289. 289:  
  290. 290:         case $operation === ':integer' && $control instanceof TextBase:
  291. 291:             return $this->getValueScript($control"res = /^-?[0-9]+$/.test(val);";
  292. 292:  
  293. 293:         case $operation === ':float' && $control instanceof TextBase:
  294. 294:             return $this->getValueScript($control"res = /^-?[0-9]*[.,]?[0-9]+$/.test(val);";
  295. 295:  
  296. 296:         case $operation === ':range' && $control instanceof TextBase:
  297. 297:             return $this->getValueScript($control"res = " ($arg[0=== NULL "true" "parseFloat(val)>=" json_encode((float) $arg[0])) " && "
  298. 298:                 . ($arg[1=== NULL "true" "parseFloat(val)<=" json_encode((float) $arg[1])) ";";
  299. 299:  
  300. 300:         case $operation === ':filled' && $control instanceof FormControl:
  301. 301:             return $this->getValueScript($control"res = val!='';";
  302. 302:  
  303. 303:         case $operation === ':valid' && $control instanceof FormControl:
  304. 304:             return $this->getValueScript($control"res = function(){\n\t" $this->getValidateScript($control->getRules()TRUE"return true; }();";
  305. 305:  
  306. 306:         case $operation === ':equal' && $control instanceof FormControl:
  307. 307:             if ($control instanceof Checkbox$arg = (bool) $arg;
  308. 308:             $tmp array();
  309. 309:             foreach ((is_array($arg$arg array($arg)) as $item{
  310. 310:                 if ($item instanceof IFormControl// compare with another form control?
  311. 311:                     $tmp["val==function(){var element;" $this->getValueScript($item)"return val;}()";
  312. 312:                 else {
  313. 313:                     $tmp["val==" json_encode($item);
  314. 314:                 }
  315. 315:             }
  316. 316:             return $this->getValueScript($control"res = (" implode(' || '$tmp");";
  317. 317:         }
  318. 318:     }
  319. 319:  
  320. 320:  
  321. 321:  
  322. 322:     public static function javascript()
  323. 323:     {
  324. 324:         return TRUE;
  325. 325:     }
  326. 326: