Namespaces

  • Latte
    • Loaders
    • Macros
    • Runtime
  • Nette
    • Application
      • Responses
      • Routers
      • UI
    • Bridges
      • ApplicationDI
      • ApplicationLatte
      • ApplicationTracy
      • CacheDI
      • CacheLatte
      • DatabaseDI
      • DatabaseTracy
      • DITracy
      • FormsDI
      • FormsLatte
      • Framework
      • HttpDI
      • HttpTracy
      • MailDI
      • ReflectionDI
      • SecurityDI
      • SecurityTracy
    • Caching
      • Storages
    • ComponentModel
    • Database
      • Conventions
      • Drivers
      • Table
    • DI
      • Config
        • Adapters
      • Extensions
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Loaders
    • Localization
    • Mail
    • Neon
    • PhpGenerator
      • Traits
    • Reflection
    • Security
    • Tokenizer
    • Utils
  • Tracy
    • Bridges
      • Nette
  • none

Classes

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

Interfaces

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