Source for file FormContainer.php
Documentation is available at FormContainer.php
6: * @copyright Copyright (c) 2004, 2010 David Grudl
7: * @license http://nettephp.com/license Nette license
8: * @link http://nettephp.com
10: * @package Nette\Forms
16: * Container for form controls.
18: * @copyright Copyright (c) 2004, 2010 David Grudl
19: * @package Nette\Forms
21: * @property-read ArrayIterator $controls
22: * @property-read Form $form
23: * @property-read bool $valid
24: * @property array $values
28: /** @var array of function(Form $sender); Occurs when the form is validated */
31: /** @var FormGroup */
39: /********************* data exchange ****************d*g**/
44: * Fill-in with default values.
45: * @param array|Traversable values used to fill the form
46: * @param bool erase other default values?
47: * @return FormContainer provides a fluent interface
52: if (!$form ||
!$form->isAnchored() ||
!$form->isSubmitted()) {
61: * Fill-in with values.
62: * @param array|Traversable values used to fill the form
63: * @param bool erase other controls?
64: * @return FormContainer provides a fluent interface
68: if ($values instanceof
Traversable) {
72: throw new InvalidArgumentException("Values must be an array, " .
gettype($values) .
" given.");
77: foreach ($iterator as $name =>
$control) {
78: $sub =
$iterator->getSubIterator();
79: if (!isset($sub->cursor)) {
80: $sub->cursor =
& $cursor;
84: $control->setValue($sub->cursor[$name]);
87: $control->setValue(NULL);
91: if ((is_array($sub->cursor) ||
$sub->cursor instanceof
ArrayAccess) &&
isset($sub->cursor[$name])) {
92: $cursor =
& $sub->cursor[$name];
105: * Returns the values submitted by the form.
111: $cursor =
& $values;
113: foreach ($iterator as $name =>
$control) {
114: $sub =
$iterator->getSubIterator();
115: if (!isset($sub->cursor)) {
116: $sub->cursor =
& $cursor;
119: $sub->cursor[$name] =
$control->getValue();
122: $cursor =
& $sub->cursor[$name];
131: /********************* validation ****************d*g**/
150: * Performs the server side validation.
156: $this->onValidate($this);
158: if (!$control->getRules()->validate()) {
166: /********************* form building ****************d*g**/
172: * @return FormContainer provides a fluent interface
183: * Adds the specified component to the IComponentContainer.
188: * @throws InvalidStateException
190: public function addComponent(IComponent $component, $name, $insertBefore =
NULL)
201: * Iterates over all form controls.
202: * @return ArrayIterator
213: * @param bool throw exception if form doesn't exist?
218: return $this->lookup('Nette\Forms\Form', $need);
223: /********************* control factories ****************d*g**/
228: * Adds single-line text input control to the form.
229: * @param string control name
230: * @param string label
231: * @param int width of the control
232: * @param int maximum number of characters the user may enter
235: public function addText($name, $label =
NULL, $cols =
NULL, $maxLength =
NULL)
237: return $this[$name] =
new TextInput($label, $cols, $maxLength);
243: * Adds single-line text input control used for sensitive input such as passwords.
244: * @param string control name
245: * @param string label
246: * @param int width of the control
247: * @param int maximum number of characters the user may enter
250: public function addPassword($name, $label =
NULL, $cols =
NULL, $maxLength =
NULL)
253: $control->setPasswordMode(TRUE);
261: * Adds multi-line text input control to the form.
262: * @param string control name
263: * @param string label
264: * @param int width of the control
265: * @param int height of the control in text lines
268: public function addTextArea($name, $label =
NULL, $cols =
40, $rows =
10)
270: return $this[$name] =
new TextArea($label, $cols, $rows);
276: * Adds control that allows the user to upload files.
277: * @param string control name
278: * @param string label
279: * @return FileUpload
289: * Adds hidden form control used to store a non-displayed value.
290: * @param string control name
291: * @return HiddenField
301: * Adds check box control to the form.
302: * @param string control name
303: * @param string caption
314: * Adds set of radio button controls to the form.
315: * @param string control name
316: * @param string label
317: * @param array options from which to choose
322: return $this[$name] =
new RadioList($label, $items);
328: * Adds select box control that allows single item selection.
329: * @param string control name
330: * @param string label
331: * @param array items from which to choose
332: * @param int number of rows that should be visible
335: public function addSelect($name, $label =
NULL, array $items =
NULL, $size =
NULL)
337: return $this[$name] =
new SelectBox($label, $items, $size);
343: * Adds select box control that allows multiple item selection.
344: * @param string control name
345: * @param string label
346: * @param array options from which to choose
347: * @param int number of rows that should be visible
348: * @return MultiSelectBox
350: public function addMultiSelect($name, $label =
NULL, array $items =
NULL, $size =
NULL)
352: return $this[$name] =
new MultiSelectBox($label, $items, $size);
358: * Adds button used to submit form.
359: * @param string control name
360: * @param string caption
361: * @return SubmitButton
371: * Adds push buttons with no default behavior.
372: * @param string control name
373: * @param string caption
384: * Adds graphical button used to submit form.
385: * @param string control name
386: * @param string URI of the image
387: * @param string alternate text for the image
388: * @return ImageButton
390: public function addImage($name, $src =
NULL, $alt =
NULL)
398: * Adds naming container to the form.
399: * @param string name
400: * @return FormContainer
406: return $this[$name] =
$control;
411: /********************* interface \ArrayAccess ****************d*g**/
416: * Adds the component to the container.
417: * @param string component name
429: * Returns component specified by name. Throws exception if component doesn't exist.
430: * @param string component name
431: * @return IComponent
432: * @throws InvalidArgumentException
442: * Does component specified by name exists?
443: * @param string component name
454: * Removes component from the container. Throws exception if component doesn't exist.
455: * @param string component name
461: if ($component !==
NULL) {