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:             $this->validate();
124:         }
125:         return !$this->getErrors();
126:     }
127: 
128: 
129:     /**
130:      * Performs the server side validation.
131:      * @param  IControl[]
132:      * @return void
133:      */
134:     public function validate(array $controls = NULL)
135:     {
136:         foreach ($controls === NULL ? $this->getControls() : $controls as $control) {
137:             $control->validate();
138:         }
139:         $this->onValidate($this);
140:         $this->validated = TRUE;
141:     }
142: 
143: 
144:     /**
145:      * Returns all validation errors.
146:      * @return array
147:      */
148:     public function getErrors()
149:     {
150:         $errors = array();
151:         foreach ($this->getControls() as $control) {
152:             $errors = array_merge($errors, $control->getErrors());
153:         }
154:         return array_unique($errors);
155:     }
156: 
157: 
158:     /********************* form building ****************d*g**/
159: 
160: 
161:     /**
162:      * @return self
163:      */
164:     public function setCurrentGroup(ControlGroup $group = NULL)
165:     {
166:         $this->currentGroup = $group;
167:         return $this;
168:     }
169: 
170: 
171:     /**
172:      * Returns current group.
173:      * @return ControlGroup
174:      */
175:     public function getCurrentGroup()
176:     {
177:         return $this->currentGroup;
178:     }
179: 
180: 
181:     /**
182:      * Adds the specified component to the IContainer.
183:      * @param  IComponent
184:      * @param  string
185:      * @param  string
186:      * @return self
187:      * @throws Nette\InvalidStateException
188:      */
189:     public function addComponent(Nette\ComponentModel\IComponent $component, $name, $insertBefore = NULL)
190:     {
191:         parent::addComponent($component, $name, $insertBefore);
192:         if ($this->currentGroup !== NULL && $component instanceof IControl) {
193:             $this->currentGroup->add($component);
194:         }
195:         return $this;
196:     }
197: 
198: 
199:     /**
200:      * Iterates over all form controls.
201:      * @return \ArrayIterator
202:      */
203:     public function getControls()
204:     {
205:         return $this->getComponents(TRUE, 'Nette\Forms\IControl');
206:     }
207: 
208: 
209:     /**
210:      * Returns form.
211:      * @param  bool   throw exception if form doesn't exist?
212:      * @return Form
213:      */
214:     public function getForm($need = TRUE)
215:     {
216:         return $this->lookup('Nette\Forms\Form', $need);
217:     }
218: 
219: 
220:     /********************* control factories ****************d*g**/
221: 
222: 
223:     /**
224:      * Adds single-line text input control to the form.
225:      * @param  string  control name
226:      * @param  string  label
227:      * @param  int  width of the control (deprecated)
228:      * @param  int  maximum number of characters the user may enter
229:      * @return Nette\Forms\Controls\TextInput
230:      */
231:     public function addText($name, $label = NULL, $cols = NULL, $maxLength = NULL)
232:     {
233:         $control = new Controls\TextInput($label, $maxLength);
234:         $control->setAttribute('size', $cols);
235:         return $this[$name] = $control;
236:     }
237: 
238: 
239:     /**
240:      * Adds single-line text input control used for sensitive input such as passwords.
241:      * @param  string  control name
242:      * @param  string  label
243:      * @param  int  width of the control (deprecated)
244:      * @param  int  maximum number of characters the user may enter
245:      * @return Nette\Forms\Controls\TextInput
246:      */
247:     public function addPassword($name, $label = NULL, $cols = NULL, $maxLength = NULL)
248:     {
249:         $control = new Controls\TextInput($label, $maxLength);
250:         $control->setAttribute('size', $cols);
251:         return $this[$name] = $control->setType('password');
252:     }
253: 
254: 
255:     /**
256:      * Adds multi-line text input control to the form.
257:      * @param  string  control name
258:      * @param  string  label
259:      * @param  int  width of the control
260:      * @param  int  height of the control in text lines
261:      * @return Nette\Forms\Controls\TextArea
262:      */
263:     public function addTextArea($name, $label = NULL, $cols = NULL, $rows = NULL)
264:     {
265:         $control = new Controls\TextArea($label);
266:         $control->setAttribute('cols', $cols)->setAttribute('rows', $rows);
267:         return $this[$name] = $control;
268:     }
269: 
270: 
271:     /**
272:      * Adds control that allows the user to upload files.
273:      * @param  string  control name
274:      * @param  string  label
275:      * @param  bool  allows to upload multiple files
276:      * @return Nette\Forms\Controls\UploadControl
277:      */
278:     public function addUpload($name, $label = NULL, $multiple = FALSE)
279:     {
280:         return $this[$name] = new Controls\UploadControl($label, $multiple);
281:     }
282: 
283: 
284:     /**
285:      * Adds hidden form control used to store a non-displayed value.
286:      * @param  string  control name
287:      * @param  mixed   default value
288:      * @return Nette\Forms\Controls\HiddenField
289:      */
290:     public function addHidden($name, $default = NULL)
291:     {
292:         $control = new Controls\HiddenField;
293:         $control->setDefaultValue($default);
294:         return $this[$name] = $control;
295:     }
296: 
297: 
298:     /**
299:      * Adds check box control to the form.
300:      * @param  string  control name
301:      * @param  string  caption
302:      * @return Nette\Forms\Controls\Checkbox
303:      */
304:     public function addCheckbox($name, $caption = NULL)
305:     {
306:         return $this[$name] = new Controls\Checkbox($caption);
307:     }
308: 
309: 
310:     /**
311:      * Adds set of radio button controls to the form.
312:      * @param  string  control name
313:      * @param  string  label
314:      * @param  array   options from which to choose
315:      * @return Nette\Forms\Controls\RadioList
316:      */
317:     public function addRadioList($name, $label = NULL, array $items = NULL)
318:     {
319:         return $this[$name] = new Controls\RadioList($label, $items);
320:     }
321: 
322: 
323:     /**
324:      * Adds set of checkbox controls to the form.
325:      * @return Nette\Forms\Controls\CheckboxList
326:      */
327:     public function addCheckboxList($name, $label = NULL, array $items = NULL)
328:     {
329:         return $this[$name] = new Controls\CheckboxList($label, $items);
330:     }
331: 
332: 
333:     /**
334:      * Adds select box control that allows single item selection.
335:      * @param  string  control name
336:      * @param  string  label
337:      * @param  array   items from which to choose
338:      * @param  int     number of rows that should be visible
339:      * @return Nette\Forms\Controls\SelectBox
340:      */
341:     public function addSelect($name, $label = NULL, array $items = NULL, $size = NULL)
342:     {
343:         $control = new Controls\SelectBox($label, $items);
344:         if ($size > 1) {
345:             $control->setAttribute('size', (int) $size);
346:         }
347:         return $this[$name] = $control;
348:     }
349: 
350: 
351:     /**
352:      * Adds select box control that allows multiple item selection.
353:      * @param  string  control name
354:      * @param  string  label
355:      * @param  array   options from which to choose
356:      * @param  int     number of rows that should be visible
357:      * @return Nette\Forms\Controls\MultiSelectBox
358:      */
359:     public function addMultiSelect($name, $label = NULL, array $items = NULL, $size = NULL)
360:     {
361:         $control = new Controls\MultiSelectBox($label, $items);
362:         if ($size > 1) {
363:             $control->setAttribute('size', (int) $size);
364:         }
365:         return $this[$name] = $control;
366:     }
367: 
368: 
369:     /**
370:      * Adds button used to submit form.
371:      * @param  string  control name
372:      * @param  string  caption
373:      * @return Nette\Forms\Controls\SubmitButton
374:      */
375:     public function addSubmit($name, $caption = NULL)
376:     {
377:         return $this[$name] = new Controls\SubmitButton($caption);
378:     }
379: 
380: 
381:     /**
382:      * Adds push buttons with no default behavior.
383:      * @param  string  control name
384:      * @param  string  caption
385:      * @return Nette\Forms\Controls\Button
386:      */
387:     public function addButton($name, $caption = NULL)
388:     {
389:         return $this[$name] = new Controls\Button($caption);
390:     }
391: 
392: 
393:     /**
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 Nette\Forms\Controls\ImageButton
399:      */
400:     public function addImage($name, $src = NULL, $alt = NULL)
401:     {
402:         return $this[$name] = new Controls\ImageButton($src, $alt);
403:     }
404: 
405: 
406:     /**
407:      * Adds naming container to the form.
408:      * @param  string  name
409:      * @return Container
410:      */
411:     public function addContainer($name)
412:     {
413:         $control = new Container;
414:         $control->currentGroup = $this->currentGroup;
415:         return $this[$name] = $control;
416:     }
417: 
418: 
419:     /********************* interface \ArrayAccess ****************d*g**/
420: 
421: 
422:     /**
423:      * Adds the component to the container.
424:      * @param  string  component name
425:      * @param  Nette\ComponentModel\IComponent
426:      * @return void
427:      */
428:     public function offsetSet($name, $component)
429:     {
430:         $this->addComponent($component, $name);
431:     }
432: 
433: 
434:     /**
435:      * Returns component specified by name. Throws exception if component doesn't exist.
436:      * @param  string  component name
437:      * @return Nette\ComponentModel\IComponent
438:      * @throws Nette\InvalidArgumentException
439:      */
440:     public function offsetGet($name)
441:     {
442:         return $this->getComponent($name, TRUE);
443:     }
444: 
445: 
446:     /**
447:      * Does component specified by name exists?
448:      * @param  string  component name
449:      * @return bool
450:      */
451:     public function offsetExists($name)
452:     {
453:         return $this->getComponent($name, FALSE) !== NULL;
454:     }
455: 
456: 
457:     /**
458:      * Removes component from the container.
459:      * @param  string  component name
460:      * @return void
461:      */
462:     public function offsetUnset($name)
463:     {
464:         $component = $this->getComponent($name, FALSE);
465:         if ($component !== NULL) {
466:             $this->removeComponent($component);
467:         }
468:     }
469: 
470: 
471:     /**
472:      * Prevents cloning.
473:      */
474:     public function __clone()
475:     {
476:         throw new Nette\NotImplementedException('Form cloning is not supported yet.');
477:     }
478: 
479: }
480: 
Nette 2.2.2 API API documentation generated by ApiGen 2.8.0