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
      • Reflection
      • Table
    • DI
      • Config
        • Adapters
      • Extensions
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Loaders
    • Localization
    • Mail
    • Neon
    • PhpGenerator
    • Reflection
    • Security
    • Utils
  • none
  • Tracy
    • Bridges
      • Nette

Classes

  • BaseControl
  • Button
  • Checkbox
  • CheckboxList
  • ChoiceControl
  • CsrfProtection
  • HiddenField
  • ImageButton
  • MultiChoiceControl
  • MultiSelectBox
  • RadioList
  • SelectBox
  • SubmitButton
  • TextArea
  • TextBase
  • TextInput
  • UploadControl
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Other releases
  • Nette homepage
  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\Controls;
  9: 
 10: use Nette;
 11: 
 12: 
 13: /**
 14:  * Choice control that allows single item selection.
 15:  *
 16:  * @property   array $items
 17:  * @property-read mixed $selectedItem
 18:  */
 19: abstract class ChoiceControl extends BaseControl
 20: {
 21:     /** @var bool */
 22:     public $checkAllowedValues = TRUE;
 23: 
 24:     /** @var array */
 25:     private $items = array();
 26: 
 27: 
 28:     public function __construct($label = NULL, array $items = NULL)
 29:     {
 30:         parent::__construct($label);
 31:         if ($items !== NULL) {
 32:             $this->setItems($items);
 33:         }
 34:     }
 35: 
 36: 
 37:     /**
 38:      * Loads HTTP data.
 39:      * @return void
 40:      */
 41:     public function loadHttpData()
 42:     {
 43:         $this->value = $this->getHttpData(Nette\Forms\Form::DATA_TEXT);
 44:         if ($this->value !== NULL) {
 45:             if (is_array($this->disabled) && isset($this->disabled[$this->value])) {
 46:                 $this->value = NULL;
 47:             } else {
 48:                 $this->value = key(array($this->value => NULL));
 49:             }
 50:         }
 51:     }
 52: 
 53: 
 54:     /**
 55:      * Sets selected item (by key).
 56:      * @param  string|int
 57:      * @return static
 58:      * @internal
 59:      */
 60:     public function setValue($value)
 61:     {
 62:         if ($this->checkAllowedValues && $value !== NULL && !array_key_exists((string) $value, $this->items)) {
 63:             $set = Nette\Utils\Strings::truncate(implode(', ', array_map(function ($s) { return var_export($s, TRUE); }, array_keys($this->items))), 70, '...');
 64:             throw new Nette\InvalidArgumentException("Value '$value' is out of allowed set [$set] in field '{$this->name}'.");
 65:         }
 66:         $this->value = $value === NULL ? NULL : key(array((string) $value => NULL));
 67:         return $this;
 68:     }
 69: 
 70: 
 71:     /**
 72:      * Returns selected key.
 73:      * @return string|int
 74:      */
 75:     public function getValue()
 76:     {
 77:         return array_key_exists($this->value, $this->items) ? $this->value : NULL;
 78:     }
 79: 
 80: 
 81:     /**
 82:      * Returns selected key (not checked).
 83:      * @return string|int
 84:      */
 85:     public function getRawValue()
 86:     {
 87:         return $this->value;
 88:     }
 89: 
 90: 
 91:     /**
 92:      * Is any item selected?
 93:      * @return bool
 94:      */
 95:     public function isFilled()
 96:     {
 97:         return $this->getValue() !== NULL;
 98:     }
 99: 
100: 
101:     /**
102:      * Sets items from which to choose.
103:      * @param  array
104:      * @param  bool
105:      * @return static
106:      */
107:     public function setItems(array $items, $useKeys = TRUE)
108:     {
109:         $this->items = $useKeys ? $items : array_combine($items, $items);
110:         return $this;
111:     }
112: 
113: 
114:     /**
115:      * Returns items from which to choose.
116:      * @return array
117:      */
118:     public function getItems()
119:     {
120:         return $this->items;
121:     }
122: 
123: 
124:     /**
125:      * Returns selected value.
126:      * @return mixed
127:      */
128:     public function getSelectedItem()
129:     {
130:         $value = $this->getValue();
131:         return $value === NULL ? NULL : $this->items[$value];
132:     }
133: 
134: 
135:     /**
136:      * Disables or enables control or items.
137:      * @param  bool|array
138:      * @return static
139:      */
140:     public function setDisabled($value = TRUE)
141:     {
142:         if (!is_array($value)) {
143:             return parent::setDisabled($value);
144:         }
145: 
146:         parent::setDisabled(FALSE);
147:         $this->disabled = array_fill_keys($value, TRUE);
148:         if (isset($this->disabled[$this->value])) {
149:             $this->value = NULL;
150:         }
151:         return $this;
152:     }
153: 
154: }
155: 
Nette 2.3-20161221 API API documentation generated by ApiGen 2.8.0