Namespaces

  • Latte
    • Loaders
    • Macros
    • Runtime
  • Nette
    • Application
      • Responses
      • Routers
      • UI
    • Bridges
      • ApplicationLatte
      • ApplicationTracy
      • CacheLatte
      • DatabaseDI
      • DatabaseTracy
      • DITracy
      • FormsLatte
      • Framework
      • HttpTracy
      • SecurityTracy
    • Caching
      • Storages
    • ComponentModel
    • Database
      • Drivers
      • Reflection
      • Table
    • DI
      • Config
        • Adapters
      • Extensions
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
    • Loaders
    • Localization
    • Mail
    • Neon
    • PhpGenerator
    • Reflection
    • Security
    • Templating
    • Utils
  • NetteModule
  • none
  • Tracy

Classes

  • Container
  • ControlGroup
  • Form
  • Helpers
  • Rule
  • Rules

Interfaces

  • IControl
  • IFormRenderer
  • ISubmitterControl
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (http://nette.org)
  5:  * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
  6:  */
  7: 
  8: namespace Nette\Forms;
  9: 
 10: use Nette;
 11: 
 12: 
 13: /**
 14:  * Container for form controls.
 15:  *
 16:  * @author     David Grudl
 17:  *
 18:  * @property-write $defaults
 19:  * @property   Nette\Utils\ArrayHash $values
 20:  * @property-read bool $valid
 21:  * @property   ControlGroup $currentGroup
 22:  * @property-read \ArrayIterator $controls
 23:  * @property-read Form $form
 24:  */
 25: class Container extends Nette\ComponentModel\Container implements \ArrayAccess
 26: {
 27:     /** @var array of function(Container $sender); Occurs when the form is validated */
 28:     public $onValidate;
 29: 
 30:     /** @var ControlGroup */
 31:     protected $currentGroup;
 32: 
 33:     /** @var bool */
 34:     private $validated;
 35: 
 36: 
 37:     /********************* data exchange ****************d*g**/
 38: 
 39: 
 40:     /**
 41:      * Fill-in with default values.
 42:      * @param  array|\Traversable  values used to fill the form
 43:      * @param  bool     erase other default values?
 44:      * @return self
 45:      */
 46:     public function setDefaults($values, $erase = FALSE)
 47:     {
 48:         $form = $this->getForm(FALSE);
 49:         if (!$form || !$form->isAnchored() || !$form->isSubmitted()) {
 50:             $this->setValues($values, $erase);
 51:         }
 52:         return $this;
 53:     }
 54: 
 55: 
 56:     /**
 57:      * Fill-in with values.
 58:      * @param  array|\Traversable  values used to fill the form
 59:      * @param  bool     erase other controls?
 60:      * @return self
 61:      */
 62:     public function setValues($values, $erase = FALSE)
 63:     {
 64:         if ($values instanceof \Traversable) {
 65:             $values = iterator_to_array($values);
 66: 
 67:         } elseif (!is_array($values)) {
 68:             throw new Nette\InvalidArgumentException(sprintf('First parameter must be an array, %s given.', gettype($values)));
 69:         }
 70: 
 71:         foreach ($this->getComponents() as $name => $control) {
 72:             if ($control instanceof IControl) {
 73:                 if (array_key_exists($name, $values)) {
 74:                     $control->setValue($values[$name]);
 75: 
 76:                 } elseif ($erase) {
 77:                     $control->setValue(NULL);
 78:                 }
 79: 
 80:             } elseif ($control instanceof Container) {
 81:                 if (array_key_exists($name, $values)) {
 82:                     $control->setValues($values[$name], $erase);
 83: 
 84:                 } elseif ($erase) {
 85:                     $control->setValues(array(), $erase);
 86:                 }
 87:             }
 88:         }
 89:         return $this;
 90:     }
 91: 
 92: 
 93:     /**
 94:      * Returns the values submitted by the form.
 95:      * @param  bool  return values as an array?
 96:      * @return Nette\Utils\ArrayHash|array
 97:      */
 98:     public function getValues($asArray = FALSE)
 99:     {
100:         $values = $asArray ? array() : new Nette\Utils\ArrayHash;
101:         foreach ($this->getComponents() as $name => $control) {
102:             if ($control instanceof IControl && !$control->isOmitted()) {
103:                 $values[$name] = $control->getValue();
104: 
105:             } elseif ($control instanceof Container) {
106:                 $values[$name] = $control->getValues($asArray);
107:             }
108:         }
109:         return $values;
110:     }
111: 
112: 
113:     /********************* validation ****************d*g**/
114: 
115: 
116:     /**
117:      * Is form valid?
118:      * @return bool
119:      */
120:     public function isValid()
121:     {
122:         if (!$this->validated) {
123:             if ($this->getErrors()) {
124:                 return FALSE;
125:             }
126:             $this->validate();
127:         }
128:         return !$this->getErrors();
129:     }
130: 
131: 
132:     /**
133:      * Performs the server side validation.
134:      * @param  IControl[]
135:      * @return void
136:      */
137:     public function validate(array $controls = NULL)
138:     {
139:         foreach ($controls === NULL ? $this->getControls() : $controls as $control) {
140:             $control->validate();
141:         }
142:         $this->onValidate($this);
143:         $this->validated = TRUE;
144:     }
145: 
146: 
147:     /**
148:      * Returns all validation errors.
149:      * @return array
150:      */
151:     public function getErrors()
152:     {
153:         $errors = array();
154:         foreach ($this->getControls() as $control) {
155:             $errors = array_merge($errors, $control->getErrors());
156:         }
157:         return array_unique($errors);
158:     }
159: 
160: 
161:     /********************* form building ****************d*g**/
162: 
163: 
164:     /**
165:      * @return self
166:      */
167:     public function setCurrentGroup(ControlGroup $group = NULL)
168:     {
169:         $this->currentGroup = $group;
170:         return $this;
171:     }
172: 
173: 
174:     /**
175:      * Returns current group.
176:      * @return ControlGroup
177:      */
178:     public function getCurrentGroup()
179:     {
180:         return $this->currentGroup;
181:     }
182: 
183: 
184:     /**
185:      * Adds the specified component to the IContainer.
186:      * @param  IComponent
187:      * @param  string
188:      * @param  string
189:      * @return self
190:      * @throws Nette\InvalidStateException
191:      */
192:     public function addComponent(Nette\ComponentModel\IComponent $component, $name, $insertBefore = NULL)
193:     {
194:         parent::addComponent($component, $name, $insertBefore);
195:         if ($this->currentGroup !== NULL && $component instanceof IControl) {
196:             $this->currentGroup->add($component);
197:         }
198:         return $this;
199:     }
200: 
201: 
202:     /**
203:      * Iterates over all form controls.
204:      * @return \ArrayIterator
205:      */
206:     public function getControls()
207:     {
208:         return $this->getComponents(TRUE, 'Nette\Forms\IControl');
209:     }
210: 
211: 
212:     /**
213:      * Returns form.
214:      * @param  bool   throw exception if form doesn't exist?
215:      * @return Form
216:      */
217:     public function getForm($need = TRUE)
218:     {
219:         return $this->lookup('Nette\Forms\Form', $need);
220:     }
221: 
222: 
223:     /********************* control factories ****************d*g**/
224: 
225: 
226:     /**
227:      * Adds single-line text input control to the form.
228:      * @param  string  control name
229:      * @param  string  label
230:      * @param  int  width of the control (deprecated)
231:      * @param  int  maximum number of characters the user may enter
232:      * @return Nette\Forms\Controls\TextInput
233:      */
234:     public function addText($name, $label = NULL, $cols = NULL, $maxLength = NULL)
235:     {
236:         $control = new Controls\TextInput($label, $maxLength);
237:         $control->setAttribute('size', $cols);
238:         return $this[$name] = $control;
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 (deprecated)
247:      * @param  int  maximum number of characters the user may enter
248:      * @return Nette\Forms\Controls\TextInput
249:      */
250:     public function addPassword($name, $label = NULL, $cols = NULL, $maxLength = NULL)
251:     {
252:         $control = new Controls\TextInput($label, $maxLength);
253:         $control->setAttribute('size', $cols);
254:         return $this[$name] = $control->setType('password');
255:     }
256: 
257: 
258:     /**
259:      * Adds multi-line text input control to the form.
260:      * @param  string  control name
261:      * @param  string  label
262:      * @param  int  width of the control
263:      * @param  int  height of the control in text lines
264:      * @return Nette\Forms\Controls\TextArea
265:      */
266:     public function addTextArea($name, $label = NULL, $cols = NULL, $rows = NULL)
267:     {
268:         $control = new Controls\TextArea($label);
269:         $control->setAttribute('cols', $cols)->setAttribute('rows', $rows);
270:         return $this[$name] = $control;
271:     }
272: 
273: 
274:     /**
275:      * Adds control that allows the user to upload files.
276:      * @param  string  control name
277:      * @param  string  label
278:      * @param  bool  allows to upload multiple files
279:      * @return Nette\Forms\Controls\UploadControl
280:      */
281:     public function addUpload($name, $label = NULL, $multiple = FALSE)
282:     {
283:         return $this[$name] = new Controls\UploadControl($label, $multiple);
284:     }
285: 
286: 
287:     /**
288:      * Adds control that allows the user to upload multiple files.
289:      * @param  string  control name
290:      * @param  string  label
291:      * @return Nette\Forms\Controls\UploadControl
292:      */
293:     public function addMultiUpload($name, $label = NULL)
294:     {
295:         return $this[$name] = new Controls\UploadControl($label, TRUE);
296:     }
297: 
298: 
299:     /**
300:      * Adds hidden form control used to store a non-displayed value.
301:      * @param  string  control name
302:      * @param  mixed   default value
303:      * @return Nette\Forms\Controls\HiddenField
304:      */
305:     public function addHidden($name, $default = NULL)
306:     {
307:         $control = new Controls\HiddenField;
308:         $control->setDefaultValue($default);
309:         return $this[$name] = $control;
310:     }
311: 
312: 
313:     /**
314:      * Adds check box control to the form.
315:      * @param  string  control name
316:      * @param  string  caption
317:      * @return Nette\Forms\Controls\Checkbox
318:      */
319:     public function addCheckbox($name, $caption = NULL)
320:     {
321:         return $this[$name] = new Controls\Checkbox($caption);
322:     }
323: 
324: 
325:     /**
326:      * Adds set of radio button controls to the form.
327:      * @param  string  control name
328:      * @param  string  label
329:      * @param  array   options from which to choose
330:      * @return Nette\Forms\Controls\RadioList
331:      */
332:     public function addRadioList($name, $label = NULL, array $items = NULL)
333:     {
334:         return $this[$name] = new Controls\RadioList($label, $items);
335:     }
336: 
337: 
338:     /**
339:      * Adds set of checkbox controls to the form.
340:      * @return Nette\Forms\Controls\CheckboxList
341:      */
342:     public function addCheckboxList($name, $label = NULL, array $items = NULL)
343:     {
344:         return $this[$name] = new Controls\CheckboxList($label, $items);
345:     }
346: 
347: 
348:     /**
349:      * Adds select box control that allows single item selection.
350:      * @param  string  control name
351:      * @param  string  label
352:      * @param  array   items from which to choose
353:      * @param  int     number of rows that should be visible
354:      * @return Nette\Forms\Controls\SelectBox
355:      */
356:     public function addSelect($name, $label = NULL, array $items = NULL, $size = NULL)
357:     {
358:         $control = new Controls\SelectBox($label, $items);
359:         if ($size > 1) {
360:             $control->setAttribute('size', (int) $size);
361:         }
362:         return $this[$name] = $control;
363:     }
364: 
365: 
366:     /**
367:      * Adds select box control that allows multiple item selection.
368:      * @param  string  control name
369:      * @param  string  label
370:      * @param  array   options from which to choose
371:      * @param  int     number of rows that should be visible
372:      * @return Nette\Forms\Controls\MultiSelectBox
373:      */
374:     public function addMultiSelect($name, $label = NULL, array $items = NULL, $size = NULL)
375:     {
376:         $control = new Controls\MultiSelectBox($label, $items);
377:         if ($size > 1) {
378:             $control->setAttribute('size', (int) $size);
379:         }
380:         return $this[$name] = $control;
381:     }
382: 
383: 
384:     /**
385:      * Adds button used to submit form.
386:      * @param  string  control name
387:      * @param  string  caption
388:      * @return Nette\Forms\Controls\SubmitButton
389:      */
390:     public function addSubmit($name, $caption = NULL)
391:     {
392:         return $this[$name] = new Controls\SubmitButton($caption);
393:     }
394: 
395: 
396:     /**
397:      * Adds push buttons with no default behavior.
398:      * @param  string  control name
399:      * @param  string  caption
400:      * @return Nette\Forms\Controls\Button
401:      */
402:     public function addButton($name, $caption = NULL)
403:     {
404:         return $this[$name] = new Controls\Button($caption);
405:     }
406: 
407: 
408:     /**
409:      * Adds graphical button used to submit form.
410:      * @param  string  control name
411:      * @param  string  URI of the image
412:      * @param  string  alternate text for the image
413:      * @return Nette\Forms\Controls\ImageButton
414:      */
415:     public function addImage($name, $src = NULL, $alt = NULL)
416:     {
417:         return $this[$name] = new Controls\ImageButton($src, $alt);
418:     }
419: 
420: 
421:     /**
422:      * Adds naming container to the form.
423:      * @param  string  name
424:      * @return Container
425:      */
426:     public function addContainer($name)
427:     {
428:         $control = new Container;
429:         $control->currentGroup = $this->currentGroup;
430:         return $this[$name] = $control;
431:     }
432: 
433: 
434:     /********************* interface \ArrayAccess ****************d*g**/
435: 
436: 
437:     /**
438:      * Adds the component to the container.
439:      * @param  string  component name
440:      * @param  Nette\ComponentModel\IComponent
441:      * @return void
442:      */
443:     public function offsetSet($name, $component)
444:     {
445:         $this->addComponent($component, $name);
446:     }
447: 
448: 
449:     /**
450:      * Returns component specified by name. Throws exception if component doesn't exist.
451:      * @param  string  component name
452:      * @return Nette\ComponentModel\IComponent
453:      * @throws Nette\InvalidArgumentException
454:      */
455:     public function offsetGet($name)
456:     {
457:         return $this->getComponent($name, TRUE);
458:     }
459: 
460: 
461:     /**
462:      * Does component specified by name exists?
463:      * @param  string  component name
464:      * @return bool
465:      */
466:     public function offsetExists($name)
467:     {
468:         return $this->getComponent($name, FALSE) !== NULL;
469:     }
470: 
471: 
472:     /**
473:      * Removes component from the container.
474:      * @param  string  component name
475:      * @return void
476:      */
477:     public function offsetUnset($name)
478:     {
479:         $component = $this->getComponent($name, FALSE);
480:         if ($component !== NULL) {
481:             $this->removeComponent($component);
482:         }
483:     }
484: 
485: 
486:     /**
487:      * Prevents cloning.
488:      */
489:     public function __clone()
490:     {
491:         throw new Nette\NotImplementedException('Form cloning is not supported yet.');
492:     }
493: 
494: }
495: 
Nette 2.2.6 API API documentation generated by ApiGen 2.8.0