Packages

  • Nette
    • Application
    • Caching
    • Collections
    • Config
    • Forms
    • IO
    • Loaders
    • Mail
    • Reflection
    • Security
    • Templates
    • Web
  • None
  • PHP

Classes

  • NButton
  • NCheckbox
  • NConventionalRenderer
  • NFileUpload
  • NForm
  • NFormContainer
  • NFormGroup
  • NHiddenField
  • NImageButton
  • NInstantClientScript
  • NMultiSelectBox
  • NRadioList
  • NRule
  • NRules
  • NSelectBox
  • NSubmitButton
  • NTextArea
  • NTextBase
  • NTextInput

Interfaces

  • IFormControl
  • IFormRenderer
  • INamingContainer
  • ISubmitterControl
  • Overview
  • Package
  • Class
  • Tree
  • Other releases
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (https://nette.org)
  5:  *
  6:  * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
  7:  *
  8:  * For the full copyright and license information, please view
  9:  * the file license.txt that was distributed with this source code.
 10:  * @package Nette\Forms
 11:  */
 12: 
 13: 
 14: 
 15: /**
 16:  * Container for form controls.
 17:  *
 18:  * @author     David Grudl
 19:  *
 20:  * @property-read ArrayIterator $controls
 21:  * @property-read NForm $form
 22:  * @property-read bool $valid
 23:  * @property   array $values
 24:  * @package Nette\Forms
 25:  */
 26: class NFormContainer extends NComponentContainer implements ArrayAccess, INamingContainer
 27: {
 28:     /** @var array of function(Form $sender); Occurs when the form is validated */
 29:     public $onValidate;
 30: 
 31:     /** @var NFormGroup */
 32:     protected $currentGroup;
 33: 
 34:     /** @var bool */
 35:     protected $valid;
 36: 
 37: 
 38: 
 39:     /********************* data exchange ****************d*g**/
 40: 
 41: 
 42: 
 43:     /**
 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 NFormContainer  provides a fluent interface
 48:      */
 49:     public function setDefaults($values, $erase = FALSE)
 50:     {
 51:         $form = $this->getForm(FALSE);
 52:         if (!$form || !$form->isAnchored() || !$form->isSubmitted()) {
 53:             $this->setValues($values, $erase);
 54:         }
 55:         return $this;
 56:     }
 57: 
 58: 
 59: 
 60:     /**
 61:      * Fill-in with values.
 62:      * @param  array|Traversable  values used to fill the form
 63:      * @param  bool     erase other controls?
 64:      * @return NFormContainer  provides a fluent interface
 65:      */
 66:     public function setValues($values, $erase = FALSE)
 67:     {
 68:         if ($values instanceof Traversable) {
 69:             $values = iterator_to_array($values);
 70: 
 71:         } elseif (!is_array($values)) {
 72:             throw new InvalidArgumentException("Values must be an array, " . gettype($values) ." given.");
 73:         }
 74: 
 75:         $cursor = & $values;
 76:         $iterator = $this->getComponents(TRUE);
 77:         foreach ($iterator as $name => $control) {
 78:             $sub = $iterator->getSubIterator();
 79:             if (!isset($sub->cursor)) {
 80:                 $sub->cursor = & $cursor;
 81:             }
 82:             if ($control instanceof IFormControl) {
 83:                 if ((is_array($sub->cursor) || $sub->cursor instanceof ArrayAccess) && array_key_exists($name, $sub->cursor)) {
 84:                     $control->setValue($sub->cursor[$name]);
 85: 
 86:                 } elseif ($erase) {
 87:                     $control->setValue(NULL);
 88:                 }
 89:             }
 90:             if ($control instanceof INamingContainer) {
 91:                 if ((is_array($sub->cursor) || $sub->cursor instanceof ArrayAccess) && isset($sub->cursor[$name])) {
 92:                     $cursor = & $sub->cursor[$name];
 93:                 } else {
 94:                     unset($cursor);
 95:                     $cursor = NULL;
 96:                 }
 97:             }
 98:         }
 99:         return $this;
100:     }
101: 
102: 
103: 
104:     /**
105:      * Returns the values submitted by the form.
106:      * @return array
107:      */
108:     public function getValues()
109:     {
110:         $values = array();
111:         $cursor = & $values;
112:         $iterator = $this->getComponents(TRUE);
113:         foreach ($iterator as $name => $control) {
114:             $sub = $iterator->getSubIterator();
115:             if (!isset($sub->cursor)) {
116:                 $sub->cursor = & $cursor;
117:             }
118:             if ($control instanceof IFormControl && !$control->isDisabled() && !($control instanceof ISubmitterControl)) {
119:                 $sub->cursor[$name] = $control->getValue();
120:             }
121:             if ($control instanceof INamingContainer) {
122:                 $cursor = & $sub->cursor[$name];
123:                 $cursor = array();
124:             }
125:         }
126:         return $values;
127:     }
128: 
129: 
130: 
131:     /********************* validation ****************d*g**/
132: 
133: 
134: 
135:     /**
136:      * Is form valid?
137:      * @return bool
138:      */
139:     public function isValid()
140:     {
141:         if ($this->valid === NULL) {
142:             $this->validate();
143:         }
144:         return $this->valid;
145:     }
146: 
147: 
148: 
149:     /**
150:      * Performs the server side validation.
151:      * @return void
152:      */
153:     public function validate()
154:     {
155:         $this->valid = TRUE;
156:         $this->onValidate($this);
157:         foreach ($this->getControls() as $control) {
158:             if (!$control->getRules()->validate()) {
159:                 $this->valid = FALSE;
160:             }
161:         }
162:     }
163: 
164: 
165: 
166:     /********************* form building ****************d*g**/
167: 
168: 
169: 
170:     /**
171:      * @param  NFormGroup
172:      * @return NFormContainer  provides a fluent interface
173:      */
174:     public function setCurrentGroup(NFormGroup $group = NULL)
175:     {
176:         $this->currentGroup = $group;
177:         return $this;
178:     }
179: 
180: 
181: 
182:     /**
183:      * Adds the specified component to the IComponentContainer.
184:      * @param  IComponent
185:      * @param  string
186:      * @param  string
187:      * @return void
188:      * @throws InvalidStateException
189:      */
190:     public function addComponent(IComponent $component, $name, $insertBefore = NULL)
191:     {
192:         parent::addComponent($component, $name, $insertBefore);
193:         if ($this->currentGroup !== NULL && $component instanceof IFormControl) {
194:             $this->currentGroup->add($component);
195:         }
196:     }
197: 
198: 
199: 
200:     /**
201:      * Iterates over all form controls.
202:      * @return ArrayIterator
203:      */
204:     public function getControls()
205:     {
206:         return $this->getComponents(TRUE, 'IFormControl');
207:     }
208: 
209: 
210: 
211:     /**
212:      * Returns form.
213:      * @param  bool   throw exception if form doesn't exist?
214:      * @return NForm
215:      */
216:     public function getForm($need = TRUE)
217:     {
218:         return $this->lookup('NForm', $need);
219:     }
220: 
221: 
222: 
223:     /********************* control factories ****************d*g**/
224: 
225: 
226: 
227:     /**
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
233:      * @return NTextInput
234:      */
235:     public function addText($name, $label = NULL, $cols = NULL, $maxLength = NULL)
236:     {
237:         return $this[$name] = new NTextInput($label, $cols, $maxLength);
238:     }
239: 
240: 
241: 
242:     /**
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
248:      * @return NTextInput
249:      */
250:     public function addPassword($name, $label = NULL, $cols = NULL, $maxLength = NULL)
251:     {
252:         $control = new NTextInput($label, $cols, $maxLength);
253:         $control->setPasswordMode(TRUE);
254:         return $this[$name] = $control;
255:     }
256: 
257: 
258: 
259:     /**
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
265:      * @return NTextArea
266:      */
267:     public function addTextArea($name, $label = NULL, $cols = 40, $rows = 10)
268:     {
269:         return $this[$name] = new NTextArea($label, $cols, $rows);
270:     }
271: 
272: 
273: 
274:     /**
275:      * Adds control that allows the user to upload files.
276:      * @param  string  control name
277:      * @param  string  label
278:      * @return NFileUpload
279:      */
280:     public function addFile($name, $label = NULL)
281:     {
282:         return $this[$name] = new NFileUpload($label);
283:     }
284: 
285: 
286: 
287:     /**
288:      * Adds hidden form control used to store a non-displayed value.
289:      * @param  string  control name
290:      * @param  mixed   default value
291:      * @return NHiddenField
292:      */
293:     public function addHidden($name, $default = NULL)
294:     {
295:         $control = new NHiddenField;
296:         $control->setDefaultValue($default);
297:         return $this[$name] = $control;
298:     }
299: 
300: 
301: 
302:     /**
303:      * Adds check box control to the form.
304:      * @param  string  control name
305:      * @param  string  caption
306:      * @return NCheckbox
307:      */
308:     public function addCheckbox($name, $caption = NULL)
309:     {
310:         return $this[$name] = new NCheckbox($caption);
311:     }
312: 
313: 
314: 
315:     /**
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
320:      * @return NRadioList
321:      */
322:     public function addRadioList($name, $label = NULL, array $items = NULL)
323:     {
324:         return $this[$name] = new NRadioList($label, $items);
325:     }
326: 
327: 
328: 
329:     /**
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
335:      * @return NSelectBox
336:      */
337:     public function addSelect($name, $label = NULL, array $items = NULL, $size = NULL)
338:     {
339:         return $this[$name] = new NSelectBox($label, $items, $size);
340:     }
341: 
342: 
343: 
344:     /**
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 NMultiSelectBox
351:      */
352:     public function addMultiSelect($name, $label = NULL, array $items = NULL, $size = NULL)
353:     {
354:         return $this[$name] = new NMultiSelectBox($label, $items, $size);
355:     }
356: 
357: 
358: 
359:     /**
360:      * Adds button used to submit form.
361:      * @param  string  control name
362:      * @param  string  caption
363:      * @return NSubmitButton
364:      */
365:     public function addSubmit($name, $caption = NULL)
366:     {
367:         return $this[$name] = new NSubmitButton($caption);
368:     }
369: 
370: 
371: 
372:     /**
373:      * Adds push buttons with no default behavior.
374:      * @param  string  control name
375:      * @param  string  caption
376:      * @return NButton
377:      */
378:     public function addButton($name, $caption)
379:     {
380:         return $this[$name] = new NButton($caption);
381:     }
382: 
383: 
384: 
385:     /**
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 NImageButton
391:      */
392:     public function addImage($name, $src = NULL, $alt = NULL)
393:     {
394:         return $this[$name] = new NImageButton($src, $alt);
395:     }
396: 
397: 
398: 
399:     /**
400:      * Adds naming container to the form.
401:      * @param  string  name
402:      * @return NFormContainer
403:      */
404:     public function addContainer($name)
405:     {
406:         $control = new NFormContainer;
407:         $control->currentGroup = $this->currentGroup;
408:         return $this[$name] = $control;
409:     }
410: 
411: 
412: 
413:     /********************* interface ArrayAccess ****************d*g**/
414: 
415: 
416: 
417:     /**
418:      * Adds the component to the container.
419:      * @param  string  component name
420:      * @param  IComponent
421:      * @return void
422:      */
423:     final public function offsetSet($name, $component)
424:     {
425:         $this->addComponent($component, $name);
426:     }
427: 
428: 
429: 
430:     /**
431:      * Returns component specified by name. Throws exception if component doesn't exist.
432:      * @param  string  component name
433:      * @return IComponent
434:      * @throws InvalidArgumentException
435:      */
436:     final public function offsetGet($name)
437:     {
438:         return $this->getComponent($name, TRUE);
439:     }
440: 
441: 
442: 
443:     /**
444:      * Does component specified by name exists?
445:      * @param  string  component name
446:      * @return bool
447:      */
448:     final public function offsetExists($name)
449:     {
450:         return $this->getComponent($name, FALSE) !== NULL;
451:     }
452: 
453: 
454: 
455:     /**
456:      * Removes component from the container.
457:      * @param  string  component name
458:      * @return void
459:      */
460:     final public function offsetUnset($name)
461:     {
462:         $component = $this->getComponent($name, FALSE);
463:         if ($component !== NULL) {
464:             $this->removeComponent($component);
465:         }
466:     }
467: 
468: 
469: 
470:     /**
471:      * Prevents cloning.
472:      */
473:     final public function __clone()
474:     {
475:         throw new NotImplementedException('Form cloning is not supported yet.');
476:     }
477: 
478: }
479: 
Nette Framework 0.9.7 (for PHP 5.2) API documentation generated by ApiGen 2.3.0