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    Nette\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    Nette\Forms
  34. 34:  *
  35. 35:  * @property-read ArrayIterator $controls 
  36. 36:  * @property-read Form $form 
  37. 37:  */
  38. 38: class FormContainer extends ComponentContainer implements ArrayAccessINamingContainer
  39. 39: {
  40. 40:     /** @var FormGroup */
  41. 41:     protected $currentGroup;
  42. 42:  
  43. 43:  
  44. 44:  
  45. 45:     /**
  46. 46:      * @param  FormGroup 
  47. 47:      * @return void 
  48. 48:      */
  49. 49:     public function setCurrentGroup(FormGroup $group NULL)
  50. 50:     {
  51. 51:         $this->currentGroup = $group;
  52. 52:     }
  53. 53:  
  54. 54:  
  55. 55:  
  56. 56:     /**
  57. 57:      * Adds the specified component to the IComponentContainer.
  58. 58:      * @param  IComponent 
  59. 59:      * @param  string 
  60. 60:      * @param  string 
  61. 61:      * @return void 
  62. 62:      * @throws InvalidStateException
  63. 63:      */
  64. 64:     public function addComponent(IComponent $component$name$insertBefore NULL)
  65. 65:     {
  66. 66:         parent::addComponent($component$name$insertBefore);
  67. 67:         if ($this->currentGroup !== NULL && $component instanceof IFormControl{
  68. 68:             $this->currentGroup->add($component);
  69. 69:         }
  70. 70:     }
  71. 71:  
  72. 72:  
  73. 73:  
  74. 74:     /**
  75. 75:      * Iterates over all form controls.
  76. 76:      * @return ArrayIterator 
  77. 77:      */
  78. 78:     public function getControls()
  79. 79:     {
  80. 80:         return $this->getComponents(TRUE'Nette\Forms\IFormControl');
  81. 81:     }
  82. 82:  
  83. 83:  
  84. 84:  
  85. 85:     /**
  86. 86:      * Returns form.
  87. 87:      * @param  bool   throw exception if form doesn't exist?
  88. 88:      * @return Form 
  89. 89:      */
  90. 90:     public function getForm($need TRUE)
  91. 91:     {
  92. 92:         return $this->lookup('Nette\Forms\Form'$need);
  93. 93:     }
  94. 94:  
  95. 95:  
  96. 96:  
  97. 97:     /********************* control factories ****************d*g**/
  98. 98:  
  99. 99:  
  100. 100:  
  101. 101:     /**
  102. 102:      * Adds single-line text input control to the form.
  103. 103:      * @param  string  control name
  104. 104:      * @param  string  label
  105. 105:      * @param  int  width of the control
  106. 106:      * @param  int  maximum number of characters the user may enter
  107. 107:      * @return TextInput 
  108. 108:      */
  109. 109:     public function addText($name$label NULL$cols NULL$maxLength NULL)
  110. 110:     {
  111. 111:         return $this[$namenew TextInput($label$cols$maxLength);
  112. 112:     }
  113. 113:  
  114. 114:  
  115. 115:  
  116. 116:     /**
  117. 117:      * Adds single-line text input control used for sensitive input such as passwords.
  118. 118:      * @param  string  control name
  119. 119:      * @param  string  label
  120. 120:      * @param  int  width of the control
  121. 121:      * @param  int  maximum number of characters the user may enter
  122. 122:      * @return TextInput 
  123. 123:      */
  124. 124:     public function addPassword($name$label NULL$cols NULL$maxLength NULL)
  125. 125:     {
  126. 126:         $control new TextInput($label$cols$maxLength);
  127. 127:         $control->setPasswordMode(TRUE);
  128. 128:         $this->addComponent($control$name);
  129. 129:         return $control;
  130. 130:     }
  131. 131:  
  132. 132:  
  133. 133:  
  134. 134:     /**
  135. 135:      * Adds multi-line text input control to the form.
  136. 136:      * @param  string  control name
  137. 137:      * @param  string  label
  138. 138:      * @param  int  width of the control
  139. 139:      * @param  int  height of the control in text lines
  140. 140:      * @return TextArea 
  141. 141:      */
  142. 142:     public function addTextArea($name$label NULL$cols 40$rows 10)
  143. 143:     {
  144. 144:         return $this[$namenew TextArea($label$cols$rows);
  145. 145:     }
  146. 146:  
  147. 147:  
  148. 148:  
  149. 149:     /**
  150. 150:      * Adds control that allows the user to upload files.
  151. 151:      * @param  string  control name
  152. 152:      * @param  string  label
  153. 153:      * @return FileUpload 
  154. 154:      */
  155. 155:     public function addFile($name$label NULL)
  156. 156:     {
  157. 157:         return $this[$namenew FileUpload($label);
  158. 158:     }
  159. 159:  
  160. 160:  
  161. 161:  
  162. 162:     /**
  163. 163:      * Adds hidden form control used to store a non-displayed value.
  164. 164:      * @param  string  control name
  165. 165:      * @return HiddenField 
  166. 166:      */
  167. 167:     public function addHidden($name)
  168. 168:     {
  169. 169:         return $this[$namenew HiddenField;
  170. 170:     }
  171. 171:  
  172. 172:  
  173. 173:  
  174. 174:     /**
  175. 175:      * Adds check box control to the form.
  176. 176:      * @param  string  control name
  177. 177:      * @param  string  caption
  178. 178:      * @return Checkbox 
  179. 179:      */
  180. 180:     public function addCheckbox($name$caption)
  181. 181:     {
  182. 182:         return $this[$namenew Checkbox($caption);
  183. 183:     }
  184. 184:  
  185. 185:  
  186. 186:  
  187. 187:     /**
  188. 188:      * Adds set of radio button controls to the form.
  189. 189:      * @param  string  control name
  190. 190:      * @param  string  label
  191. 191:      * @param  array   options from which to choose
  192. 192:      * @return RadioList 
  193. 193:      */
  194. 194:     public function addRadioList($name$label NULLarray $items NULL)
  195. 195:     {
  196. 196:         return $this[$namenew RadioList($label$items);
  197. 197:     }
  198. 198:  
  199. 199:  
  200. 200:  
  201. 201:     /**
  202. 202:      * Adds select box control that allows single item selection.
  203. 203:      * @param  string  control name
  204. 204:      * @param  string  label
  205. 205:      * @param  array   items from which to choose
  206. 206:      * @param  int     number of rows that should be visible
  207. 207:      * @return SelectBox 
  208. 208:      */
  209. 209:     public function addSelect($name$label NULLarray $items NULL$size NULL)
  210. 210:     {
  211. 211:         return $this[$namenew SelectBox($label$items$size);
  212. 212:     
  213. 213:  
  214. 214:  
  215. 215:  
  216. 216:     /**
  217. 217:      * Adds select box control that allows multiple item selection.
  218. 218:      * @param  string  control name
  219. 219:      * @param  string  label
  220. 220:      * @param  array   options from which to choose
  221. 221:      * @param  int     number of rows that should be visible
  222. 222:      * @return MultiSelectBox 
  223. 223:      */
  224. 224:     public function addMultiSelect($name$label NULLarray $items NULL$size NULL)
  225. 225:     {
  226. 226:         return $this[$namenew MultiSelectBox($label$items$size);
  227. 227:     
  228. 228:  
  229. 229:  
  230. 230:  
  231. 231:     /**
  232. 232:      * Adds button used to submit form.
  233. 233:      * @param  string  control name
  234. 234:      * @param  string  caption
  235. 235:      * @return SubmitButton 
  236. 236:      */
  237. 237:     public function addSubmit($name$caption)
  238. 238:     {
  239. 239:         return $this[$namenew SubmitButton($caption);
  240. 240:     }
  241. 241:  
  242. 242:  
  243. 243:  
  244. 244:     /**
  245. 245:      * Adds push buttons with no default behavior.
  246. 246:      * @param  string  control name
  247. 247:      * @param  string  caption
  248. 248:      * @return Button 
  249. 249:      */
  250. 250:     public function addButton($name$caption)
  251. 251:     {
  252. 252:         return $this[$namenew Button($caption);
  253. 253:     }
  254. 254:  
  255. 255:  
  256. 256:  
  257. 257:     /**
  258. 258:      * Adds graphical button used to submit form.
  259. 259:      * @param  string  control name
  260. 260:      * @param  string  URI of the image
  261. 261:      * @param  string  alternate text for the image
  262. 262:      * @return ImageButton 
  263. 263:      */
  264. 264:     public function addImage($name$src NULL$alt NULL)
  265. 265:     {
  266. 266:         return $this[$namenew ImageButton($src$alt);
  267. 267:     }
  268. 268:  
  269. 269:  
  270. 270:  
  271. 271:     /**
  272. 272:      * Adds naming container to the form.
  273. 273:      * @param  string  name
  274. 274:      * @return FormContainer 
  275. 275:      */
  276. 276:     public function addContainer($name)
  277. 277:     {
  278. 278:         $control new FormContainer;
  279. 279:         $control->currentGroup $this->currentGroup;
  280. 280:         return $this[$name$control;
  281. 281:     }
  282. 282:  
  283. 283:  
  284. 284:  
  285. 285:     /**
  286. 286:      * Adds control that repeats a specified prototype for each item in the list.
  287. 287:      * @param  string  control name
  288. 288:      * @return RepeaterControl 
  289. 289:      */
  290. 290:     public function addRepeater($name)
  291. 291:     {
  292. 292:         return $this[$namenew RepeaterControl;
  293. 293:     }
  294. 294:  
  295. 295:  
  296. 296:  
  297. 297:     /********************* interface \ArrayAccess ****************d*g**/
  298. 298:  
  299. 299:  
  300. 300:  
  301. 301:     /**
  302. 302:      * Adds the component to the container.
  303. 303:      * @param  string  component name
  304. 304:      * @param  IComponent 
  305. 305:      * @return void. 
  306. 306:      */
  307. 307:     final public function offsetSet($name$component)
  308. 308:     {
  309. 309:         $this->addComponent($component$name);
  310. 310:     }
  311. 311:  
  312. 312:  
  313. 313:  
  314. 314:     /**
  315. 315:      * Returns component specified by name. Throws exception if component doesn't exist.
  316. 316:      * @param  string  component name
  317. 317:      * @return IComponent 
  318. 318:      * @throws InvalidArgumentException
  319. 319:      */
  320. 320:     final public function offsetGet($name)
  321. 321:     {
  322. 322:         return $this->getComponent($nameTRUE);
  323. 323:     }
  324. 324:  
  325. 325:  
  326. 326:  
  327. 327:     /**
  328. 328:      * Does component specified by name exists?
  329. 329:      * @param  string  component name
  330. 330:      * @return bool 
  331. 331:      */
  332. 332:     final public function offsetExists($name)
  333. 333:     {
  334. 334:         return $this->getComponent($nameFALSE!== NULL;
  335. 335:     }
  336. 336:  
  337. 337:  
  338. 338:  
  339. 339:     /**
  340. 340:      * Removes component from the container. Throws exception if component doesn't exist.
  341. 341:      * @param  string  component name
  342. 342:      * @return void 
  343. 343:      */
  344. 344:     final public function offsetUnset($name)
  345. 345:     {
  346. 346:         $component $this->getComponent($nameFALSE);
  347. 347:         if ($component !== NULL{
  348. 348:             $this->removeComponent($component);
  349. 349:         }
  350. 350:     }
  351. 351: