Source for file FormContainer.php
Documentation is available at FormContainer.php
6: * Copyright (c) 2004, 2009 David Grudl (http://davidgrudl.com)
8: * This source file is subject to the "Nette license" that is bundled
9: * with this package in the file license.txt.
11: * For more information please see http://nettephp.com
13: * @copyright Copyright (c) 2004, 2009 David Grudl
14: * @license http://nettephp.com/license Nette license
15: * @link http://nettephp.com
17: * @package Nette\Forms
22: require_once dirname(__FILE__) .
'/../ComponentContainer.php';
24: require_once dirname(__FILE__) .
'/../Forms/INamingContainer.php';
29: * Container for form controls.
31: * @author David Grudl
32: * @copyright Copyright (c) 2004, 2009 David Grudl
33: * @package Nette\Forms
35: * @property-read ArrayIterator $controls
36: * @property-read Form $form
37: * @property-read bool $valid
38: * @property array $values
42: /** @var FormGroup */
50: /********************* data exchange ****************d*g**/
55: * Fill-in with default values.
56: * @param array|Traversable values used to fill the form
57: * @param bool erase other default values?
58: * @return FormContainer provides a fluent interface
63: if (!$form ||
!$form->isAnchored() ||
!$form->isSubmitted()) {
72: * Fill-in with values.
73: * @param array|Traversable values used to fill the form
74: * @param bool erase other controls?
75: * @return FormContainer provides a fluent interface
79: if ($values instanceof
Traversable) {
83: throw new InvalidArgumentException("Values must be an array, " .
gettype($values) .
" given.");
88: foreach ($iterator as $name =>
$control) {
89: $sub =
$iterator->getSubIterator();
90: if (!isset($sub->cursor)) {
91: $sub->cursor =
& $cursor;
95: $control->setValue($sub->cursor[$name]);
98: $control->setValue(NULL);
102: if ((is_array($sub->cursor) ||
$sub->cursor instanceof
ArrayAccess) &&
isset($sub->cursor[$name])) {
103: $cursor =
& $sub->cursor[$name];
116: * Returns the values submitted by the form.
122: $cursor =
& $values;
124: foreach ($iterator as $name =>
$control) {
125: $sub =
$iterator->getSubIterator();
126: if (!isset($sub->cursor)) {
127: $sub->cursor =
& $cursor;
130: $sub->cursor[$name] =
$control->getValue();
133: $cursor =
& $sub->cursor[$name];
142: /********************* validation ****************d*g**/
161: * Performs the server side validation.
168: if (!$control->getRules()->validate()) {
176: /********************* form building ****************d*g**/
182: * @return FormContainer provides a fluent interface
193: * Adds the specified component to the IComponentContainer.
198: * @throws InvalidStateException
200: public function addComponent(IComponent $component, $name, $insertBefore =
NULL)
211: * Iterates over all form controls.
212: * @return ArrayIterator
223: * @param bool throw exception if form doesn't exist?
228: return $this->lookup('Nette\Forms\Form', $need);
233: /********************* control factories ****************d*g**/
238: * Adds single-line text input control to the form.
239: * @param string control name
240: * @param string label
241: * @param int width of the control
242: * @param int maximum number of characters the user may enter
245: public function addText($name, $label =
NULL, $cols =
NULL, $maxLength =
NULL)
247: return $this[$name] =
new TextInput($label, $cols, $maxLength);
253: * Adds single-line text input control used for sensitive input such as passwords.
254: * @param string control name
255: * @param string label
256: * @param int width of the control
257: * @param int maximum number of characters the user may enter
260: public function addPassword($name, $label =
NULL, $cols =
NULL, $maxLength =
NULL)
263: $control->setPasswordMode(TRUE);
271: * Adds multi-line text input control to the form.
272: * @param string control name
273: * @param string label
274: * @param int width of the control
275: * @param int height of the control in text lines
278: public function addTextArea($name, $label =
NULL, $cols =
40, $rows =
10)
280: return $this[$name] =
new TextArea($label, $cols, $rows);
286: * Adds control that allows the user to upload files.
287: * @param string control name
288: * @param string label
289: * @return FileUpload
299: * Adds hidden form control used to store a non-displayed value.
300: * @param string control name
301: * @return HiddenField
311: * Adds check box control to the form.
312: * @param string control name
313: * @param string caption
324: * Adds set of radio button controls to the form.
325: * @param string control name
326: * @param string label
327: * @param array options from which to choose
332: return $this[$name] =
new RadioList($label, $items);
338: * Adds select box control that allows single item selection.
339: * @param string control name
340: * @param string label
341: * @param array items from which to choose
342: * @param int number of rows that should be visible
345: public function addSelect($name, $label =
NULL, array $items =
NULL, $size =
NULL)
347: return $this[$name] =
new SelectBox($label, $items, $size);
353: * Adds select box control that allows multiple item selection.
354: * @param string control name
355: * @param string label
356: * @param array options from which to choose
357: * @param int number of rows that should be visible
358: * @return MultiSelectBox
360: public function addMultiSelect($name, $label =
NULL, array $items =
NULL, $size =
NULL)
362: return $this[$name] =
new MultiSelectBox($label, $items, $size);
368: * Adds button used to submit form.
369: * @param string control name
370: * @param string caption
371: * @return SubmitButton
381: * Adds push buttons with no default behavior.
382: * @param string control name
383: * @param string caption
394: * Adds graphical button used to submit form.
395: * @param string control name
396: * @param string URI of the image
397: * @param string alternate text for the image
398: * @return ImageButton
400: public function addImage($name, $src =
NULL, $alt =
NULL)
408: * Adds naming container to the form.
409: * @param string name
410: * @return FormContainer
416: return $this[$name] =
$control;
422: * Adds control that repeats a specified prototype for each item in the list.
423: * @param string control name
424: * @return RepeaterControl
433: /********************* interface \ArrayAccess ****************d*g**/
438: * Adds the component to the container.
439: * @param string component name
451: * Returns component specified by name. Throws exception if component doesn't exist.
452: * @param string component name
453: * @return IComponent
454: * @throws InvalidArgumentException
464: * Does component specified by name exists?
465: * @param string component name
476: * Removes component from the container. Throws exception if component doesn't exist.
477: * @param string component name
483: if ($component !==
NULL) {