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);
254: return $this[$name] =
$control;
260: * Adds multi-line text input control to the form.
261: * @param string control name
262: * @param string label
263: * @param int width of the control
264: * @param int height of the control in text lines
267: public function addTextArea($name, $label =
NULL, $cols =
40, $rows =
10)
269: return $this[$name] =
new TextArea($label, $cols, $rows);
275: * Adds control that allows the user to upload files.
276: * @param string control name
277: * @param string label
278: * @return FileUpload
288: * Adds hidden form control used to store a non-displayed value.
289: * @param string control name
290: * @param mixed default value
291: * @return HiddenField
296: $control->setDefaultValue($default);
297: return $this[$name] =
$control;
303: * Adds check box control to the form.
304: * @param string control name
305: * @param string caption
316: * Adds set of radio button controls to the form.
317: * @param string control name
318: * @param string label
319: * @param array options from which to choose
324: return $this[$name] =
new RadioList($label, $items);
330: * Adds select box control that allows single item selection.
331: * @param string control name
332: * @param string label
333: * @param array items from which to choose
334: * @param int number of rows that should be visible
337: public function addSelect($name, $label =
NULL, array $items =
NULL, $size =
NULL)
339: return $this[$name] =
new SelectBox($label, $items, $size);
345: * Adds select box control that allows multiple item selection.
346: * @param string control name
347: * @param string label
348: * @param array options from which to choose
349: * @param int number of rows that should be visible
350: * @return MultiSelectBox
352: public function addMultiSelect($name, $label =
NULL, array $items =
NULL, $size =
NULL)
354: return $this[$name] =
new MultiSelectBox($label, $items, $size);
360: * Adds button used to submit form.
361: * @param string control name
362: * @param string caption
363: * @return SubmitButton
373: * Adds push buttons with no default behavior.
374: * @param string control name
375: * @param string caption
386: * Adds graphical button used to submit form.
387: * @param string control name
388: * @param string URI of the image
389: * @param string alternate text for the image
390: * @return ImageButton
392: public function addImage($name, $src =
NULL, $alt =
NULL)
400: * Adds naming container to the form.
401: * @param string name
402: * @return FormContainer
408: return $this[$name] =
$control;
413: /********************* interface \ArrayAccess ****************d*g**/
418: * Adds the component to the container.
419: * @param string component name
431: * Returns component specified by name. Throws exception if component doesn't exist.
432: * @param string component name
433: * @return IComponent
434: * @throws InvalidArgumentException
444: * Does component specified by name exists?
445: * @param string component name
456: * Removes component from the container. Throws exception if component doesn't exist.
457: * @param string component name
463: if ($component !==
NULL) {