Source for file FormContainer.php

Documentation is available at FormContainer.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 http://nettephp.com
  12. 12:  *
  13. 13:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  14. 14:  * @license    http://nettephp.com/license  Nette license
  15. 15:  * @link       http://nettephp.com
  16. 16:  * @category   Nette
  17. 17:  * @package    Forms
  18. 18:  */
  19. 19:  
  20. 20:  
  21. 21:  
  22. 22: require_once dirname(__FILE__'/../ComponentContainer.php';
  23. 23:  
  24. 24: require_once dirname(__FILE__'/../Forms/INamingContainer.php';
  25. 25:  
  26. 26:  
  27. 27:  
  28. 28: /**
  29. 29:  * Container for form controls.
  30. 30:  *
  31. 31:  * @author     David Grudl
  32. 32:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  33. 33:  * @package    Forms
  34. 34:  *
  35. 35:  * @property-read ArrayIterator $controls 
  36. 36:  * @property-read NForm $form 
  37. 37:  * @property-read bool $valid 
  38. 38:  * @property   array $values 
  39. 39:  */
  40. 40: class NFormContainer extends NComponentContainer implements ArrayAccessINamingContainer
  41. 41: {
  42. 42:     /** @var NFormGroup */
  43. 43:     protected $currentGroup;
  44. 44:  
  45. 45:     /** @var bool */
  46. 46:     protected $valid;
  47. 47:  
  48. 48:  
  49. 49:  
  50. 50:     /********************* data exchange ****************d*g**/
  51. 51:  
  52. 52:  
  53. 53:  
  54. 54:     /**
  55. 55:      * Fill-in with default values.
  56. 56:      * @param  array|Traversable values used to fill the form
  57. 57:      * @param  bool     erase other default values?
  58. 58:      * @return NFormContainer  provides a fluent interface
  59. 59:      */
  60. 60:     public function setDefaults($values$erase FALSE)
  61. 61:     {
  62. 62:         if (!$this->getForm()->isSubmitted()) {
  63. 63:             $this->setValues($values$erase);
  64. 64:         }
  65. 65:         return $this;
  66. 66:     }
  67. 67:  
  68. 68:  
  69. 69:  
  70. 70:     /**
  71. 71:      * Fill-in with values.
  72. 72:      * @param  array|Traversable values used to fill the form
  73. 73:      * @param  bool     erase other controls?
  74. 74:      * @return NFormContainer  provides a fluent interface
  75. 75:      */
  76. 76:     public function setValues($values$erase FALSE)
  77. 77:     {
  78. 78:         if ($values instanceof Traversable{
  79. 79:             $values iterator_to_array($values);
  80. 80:  
  81. 81:         elseif (!is_array($values)) {
  82. 82:             throw new InvalidArgumentException("Values must be an array, " gettype($values." given.");
  83. 83:         }
  84. 84:  
  85. 85:         $cursor $values;
  86. 86:         $iterator $this->getComponents(TRUE);
  87. 87:         foreach ($iterator as $name => $control{
  88. 88:             $sub $iterator->getSubIterator();
  89. 89:             if (!isset($sub->cursor)) {
  90. 90:                 $sub->cursor $cursor;
  91. 91:             }
  92. 92:             if ($control instanceof IFormControl{
  93. 93:                 if ((is_array($sub->cursor|| $sub->cursor instanceof ArrayAccess&& array_key_exists($name$sub->cursor)) {
  94. 94:                     $control->setValue($sub->cursor[$name]);
  95. 95:  
  96. 96:                 elseif ($erase{
  97. 97:                     $control->setValue(NULL);
  98. 98:                 }
  99. 99:             }
  100. 100:             if ($control instanceof INamingContainer{
  101. 101:                 if ((is_array($sub->cursor|| $sub->cursor instanceof ArrayAccess&& isset($sub->cursor[$name])) {
  102. 102:                     $cursor $sub->cursor[$name];
  103. 103:                 else {
  104. 104:                     unset($cursor);
  105. 105:                     $cursor NULL;
  106. 106:                 }
  107. 107:             }
  108. 108:         }
  109. 109:         return $this;
  110. 110:     }
  111. 111:  
  112. 112:  
  113. 113:  
  114. 114:     /**
  115. 115:      * Returns the values submitted by the form.
  116. 116:      * @return array 
  117. 117:      */
  118. 118:     public function getValues()
  119. 119:     {
  120. 120:         $values array();
  121. 121:         $cursor $values;
  122. 122:         $iterator $this->getComponents(TRUE);
  123. 123:         foreach ($iterator as $name => $control{
  124. 124:             $sub $iterator->getSubIterator();
  125. 125:             if (!isset($sub->cursor)) {
  126. 126:                 $sub->cursor $cursor;
  127. 127:             }
  128. 128:             if ($control instanceof IFormControl && !$control->isDisabled(&& !($control instanceof ISubmitterControl)) {
  129. 129:                 $sub->cursor[$name$control->getValue();
  130. 130:             }
  131. 131:             if ($control instanceof INamingContainer{
  132. 132:                 $cursor $sub->cursor[$name];
  133. 133:                 $cursor array();
  134. 134:             }
  135. 135:         }
  136. 136:         return $values;
  137. 137:     }
  138. 138:  
  139. 139:  
  140. 140:  
  141. 141:     /********************* validation ****************d*g**/
  142. 142:  
  143. 143:  
  144. 144:  
  145. 145:     /**
  146. 146:      * Is form valid?
  147. 147:      * @return bool 
  148. 148:      */
  149. 149:     public function isValid()
  150. 150:     {
  151. 151:         if ($this->valid === NULL{
  152. 152:             $this->validate();
  153. 153:         }
  154. 154:         return $this->valid;
  155. 155:     }
  156. 156:  
  157. 157:  
  158. 158:  
  159. 159:     /**
  160. 160:      * Performs the server side validation.
  161. 161:      * @return void 
  162. 162:      */
  163. 163:     public function validate()
  164. 164:     {
  165. 165:         $this->valid = TRUE;
  166. 166:         foreach ($this->getControls(as $control{
  167. 167:             if (!$control->getRules()->validate()) {
  168. 168:                 $this->valid = FALSE;
  169. 169:             }
  170. 170:         }
  171. 171:     }
  172. 172:  
  173. 173:  
  174. 174:  
  175. 175:     /********************* form building ****************d*g**/
  176. 176:  
  177. 177:  
  178. 178:  
  179. 179:     /**
  180. 180:      * @param  NFormGroup 
  181. 181:      * @return NFormContainer  provides a fluent interface
  182. 182:      */
  183. 183:     public function setCurrentGroup(NFormGroup $group NULL)
  184. 184:     {
  185. 185:         $this->currentGroup = $group;
  186. 186:         return $this;
  187. 187:     }
  188. 188:  
  189. 189:  
  190. 190:  
  191. 191:     /**
  192. 192:      * Adds the specified component to the IComponentContainer.
  193. 193:      * @param  IComponent 
  194. 194:      * @param  string 
  195. 195:      * @param  string 
  196. 196:      * @return void 
  197. 197:      * @throws InvalidStateException
  198. 198:      */
  199. 199:     public function addComponent(IComponent $component$name$insertBefore NULL)
  200. 200:     {
  201. 201:         parent::addComponent($component$name$insertBefore);
  202. 202:         if ($this->currentGroup !== NULL && $component instanceof IFormControl{
  203. 203:             $this->currentGroup->add($component);
  204. 204:         }
  205. 205:     }
  206. 206:  
  207. 207:  
  208. 208:  
  209. 209:     /**
  210. 210:      * Iterates over all form controls.
  211. 211:      * @return ArrayIterator 
  212. 212:      */
  213. 213:     public function getControls()
  214. 214:     {
  215. 215:         return $this->getComponents(TRUE'IFormControl');
  216. 216:     }
  217. 217:  
  218. 218:  
  219. 219:  
  220. 220:     /**
  221. 221:      * Returns form.
  222. 222:      * @param  bool   throw exception if form doesn't exist?
  223. 223:      * @return NForm 
  224. 224:      */
  225. 225:     public function getForm($need TRUE)
  226. 226:     {
  227. 227:         return $this->lookup('NForm'$need);
  228. 228:     }
  229. 229:  
  230. 230:  
  231. 231:  
  232. 232:     /********************* control factories ****************d*g**/
  233. 233:  
  234. 234:  
  235. 235:  
  236. 236:     /**
  237. 237:      * Adds single-line text input control to the form.
  238. 238:      * @param  string  control name
  239. 239:      * @param  string  label
  240. 240:      * @param  int  width of the control
  241. 241:      * @param  int  maximum number of characters the user may enter
  242. 242:      * @return NTextInput 
  243. 243:      */
  244. 244:     public function addText($name$label NULL$cols NULL$maxLength NULL)
  245. 245:     {
  246. 246:         return $this[$namenew NTextInput($label$cols$maxLength);
  247. 247:     }
  248. 248:  
  249. 249:  
  250. 250:  
  251. 251:     /**
  252. 252:      * Adds single-line text input control used for sensitive input such as passwords.
  253. 253:      * @param  string  control name
  254. 254:      * @param  string  label
  255. 255:      * @param  int  width of the control
  256. 256:      * @param  int  maximum number of characters the user may enter
  257. 257:      * @return NTextInput 
  258. 258:      */
  259. 259:     public function addPassword($name$label NULL$cols NULL$maxLength NULL)
  260. 260:     {
  261. 261:         $control new NTextInput($label$cols$maxLength);
  262. 262:         $control->setPasswordMode(TRUE);
  263. 263:         $this->addComponent($control$name);
  264. 264:         return $control;
  265. 265:     }
  266. 266:  
  267. 267:  
  268. 268:  
  269. 269:     /**
  270. 270:      * Adds multi-line text input control to the form.
  271. 271:      * @param  string  control name
  272. 272:      * @param  string  label
  273. 273:      * @param  int  width of the control
  274. 274:      * @param  int  height of the control in text lines
  275. 275:      * @return NTextArea 
  276. 276:      */
  277. 277:     public function addTextArea($name$label NULL$cols 40$rows 10)
  278. 278:     {
  279. 279:         return $this[$namenew NTextArea($label$cols$rows);
  280. 280:     }
  281. 281:  
  282. 282:  
  283. 283:  
  284. 284:     /**
  285. 285:      * Adds control that allows the user to upload files.
  286. 286:      * @param  string  control name
  287. 287:      * @param  string  label
  288. 288:      * @return NFileUpload 
  289. 289:      */
  290. 290:     public function addFile($name$label NULL)
  291. 291:     {
  292. 292:         return $this[$namenew NFileUpload($label);
  293. 293:     }
  294. 294:  
  295. 295:  
  296. 296:  
  297. 297:     /**
  298. 298:      * Adds hidden form control used to store a non-displayed value.
  299. 299:      * @param  string  control name
  300. 300:      * @return NHiddenField 
  301. 301:      */
  302. 302:     public function addHidden($name)
  303. 303:     {
  304. 304:         return $this[$namenew NHiddenField;
  305. 305:     }
  306. 306:  
  307. 307:  
  308. 308:  
  309. 309:     /**
  310. 310:      * Adds check box control to the form.
  311. 311:      * @param  string  control name
  312. 312:      * @param  string  caption
  313. 313:      * @return NCheckbox 
  314. 314:      */
  315. 315:     public function addCheckbox($name$caption)
  316. 316:     {
  317. 317:         return $this[$namenew NCheckbox($caption);
  318. 318:     }
  319. 319:  
  320. 320:  
  321. 321:  
  322. 322:     /**
  323. 323:      * Adds set of radio button controls to the form.
  324. 324:      * @param  string  control name
  325. 325:      * @param  string  label
  326. 326:      * @param  array   options from which to choose
  327. 327:      * @return NRadioList 
  328. 328:      */
  329. 329:     public function addRadioList($name$label NULLarray $items NULL)
  330. 330:     {
  331. 331:         return $this[$namenew NRadioList($label$items);
  332. 332:     }
  333. 333:  
  334. 334:  
  335. 335:  
  336. 336:     /**
  337. 337:      * Adds select box control that allows single item selection.
  338. 338:      * @param  string  control name
  339. 339:      * @param  string  label
  340. 340:      * @param  array   items from which to choose
  341. 341:      * @param  int     number of rows that should be visible
  342. 342:      * @return NSelectBox 
  343. 343:      */
  344. 344:     public function addSelect($name$label NULLarray $items NULL$size NULL)
  345. 345:     {
  346. 346:         return $this[$namenew NSelectBox($label$items$size);
  347. 347:     
  348. 348:  
  349. 349:  
  350. 350:  
  351. 351:     /**
  352. 352:      * Adds select box control that allows multiple item selection.
  353. 353:      * @param  string  control name
  354. 354:      * @param  string  label
  355. 355:      * @param  array   options from which to choose
  356. 356:      * @param  int     number of rows that should be visible
  357. 357:      * @return NMultiSelectBox 
  358. 358:      */
  359. 359:     public function addMultiSelect($name$label NULLarray $items NULL$size NULL)
  360. 360:     {
  361. 361:         return $this[$namenew NMultiSelectBox($label$items$size);
  362. 362:     
  363. 363:  
  364. 364:  
  365. 365:  
  366. 366:     /**
  367. 367:      * Adds button used to submit form.
  368. 368:      * @param  string  control name
  369. 369:      * @param  string  caption
  370. 370:      * @return NSubmitButton 
  371. 371:      */
  372. 372:     public function addSubmit($name$caption)
  373. 373:     {
  374. 374:         return $this[$namenew NSubmitButton($caption);
  375. 375:     }
  376. 376:  
  377. 377:  
  378. 378:  
  379. 379:     /**
  380. 380:      * Adds push buttons with no default behavior.
  381. 381:      * @param  string  control name
  382. 382:      * @param  string  caption
  383. 383:      * @return NButton 
  384. 384:      */
  385. 385:     public function addButton($name$caption)
  386. 386:     {
  387. 387:         return $this[$namenew NButton($caption);
  388. 388:     }
  389. 389:  
  390. 390:  
  391. 391:  
  392. 392:     /**
  393. 393:      * Adds graphical button used to submit form.
  394. 394:      * @param  string  control name
  395. 395:      * @param  string  URI of the image
  396. 396:      * @param  string  alternate text for the image
  397. 397:      * @return NImageButton 
  398. 398:      */
  399. 399:     public function addImage($name$src NULL$alt NULL)
  400. 400:     {
  401. 401:         return $this[$namenew NImageButton($src$alt);
  402. 402:     }
  403. 403:  
  404. 404:  
  405. 405:  
  406. 406:     /**
  407. 407:      * Adds naming container to the form.
  408. 408:      * @param  string  name
  409. 409:      * @return NFormContainer 
  410. 410:      */
  411. 411:     public function addContainer($name)
  412. 412:     {
  413. 413:         $control new NFormContainer;
  414. 414:         $control->currentGroup $this->currentGroup;
  415. 415:         return $this[$name$control;
  416. 416:     }
  417. 417:  
  418. 418:  
  419. 419:  
  420. 420:     /**
  421. 421:      * Adds control that repeats a specified prototype for each item in the list.
  422. 422:      * @param  string  control name
  423. 423:      * @return NRepeaterControl 
  424. 424:      */
  425. 425:     public function addRepeater($name)
  426. 426:     {
  427. 427:         return $this[$namenew NRepeaterControl;
  428. 428:     }
  429. 429:  
  430. 430:  
  431. 431:  
  432. 432:     /********************* interface \ArrayAccess ****************d*g**/
  433. 433:  
  434. 434:  
  435. 435:  
  436. 436:     /**
  437. 437:      * Adds the component to the container.
  438. 438:      * @param  string  component name
  439. 439:      * @param  IComponent 
  440. 440:      * @return void. 
  441. 441:      */
  442. 442:     final public function offsetSet($name$component)
  443. 443:     {
  444. 444:         $this->addComponent($component$name);
  445. 445:     }
  446. 446:  
  447. 447:  
  448. 448:  
  449. 449:     /**
  450. 450:      * Returns component specified by name. Throws exception if component doesn't exist.
  451. 451:      * @param  string  component name
  452. 452:      * @return IComponent 
  453. 453:      * @throws InvalidArgumentException
  454. 454:      */
  455. 455:     final public function offsetGet($name)
  456. 456:     {
  457. 457:         return $this->getComponent($nameTRUE);
  458. 458:     }
  459. 459:  
  460. 460:  
  461. 461:  
  462. 462:     /**
  463. 463:      * Does component specified by name exists?
  464. 464:      * @param  string  component name
  465. 465:      * @return bool 
  466. 466:      */
  467. 467:     final public function offsetExists($name)
  468. 468:     {
  469. 469:         return $this->getComponent($nameFALSE!== NULL;
  470. 470:     }
  471. 471:  
  472. 472:  
  473. 473:  
  474. 474:     /**
  475. 475:      * Removes component from the container. Throws exception if component doesn't exist.
  476. 476:      * @param  string  component name
  477. 477:      * @return void 
  478. 478:      */
  479. 479:     final public function offsetUnset($name)
  480. 480:     {
  481. 481:         $component $this->getComponent($nameFALSE);
  482. 482:         if ($component !== NULL{
  483. 483:             $this->removeComponent($component);
  484. 484:         }
  485. 485:     }
  486. 486: