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