Namespaces

  • Nette
    • Application
      • Diagnostics
      • Responses
      • Routers
      • UI
    • Caching
      • Storages
    • ComponentModel
    • Database
      • Diagnostics
      • Drivers
      • Reflection
      • Table
    • DI
      • Config
        • Adapters
      • Diagnostics
      • Extensions
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
      • Diagnostics
    • Iterators
    • Latte
      • Macros
    • Loaders
    • Localization
    • Mail
    • PhpGenerator
    • Reflection
    • Security
      • Diagnostics
    • Templating
    • Utils
  • NetteModule
  • none

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:  * @author     David Grudl
 17:  *
 18:  * @property   array $items
 19:  * @property-read mixed $selectedItem
 20:  * @property-read mixed $rawValue
 21:  */
 22: abstract class ChoiceControl extends BaseControl
 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  scalar
 57:      * @return self
 58:      */
 59:     public function setValue($value)
 60:     {
 61:         if ($value !== NULL && !array_key_exists((string) $value, $this->items)) {
 62:             $range = Nette\Utils\Strings::truncate(implode(', ', array_map(function ($s) { return var_export($s, TRUE); }, array_keys($this->items))), 70, '...');
 63:             throw new Nette\InvalidArgumentException("Value '$value' is out of allowed range [$range] in field '{$this->name}'.");
 64:         }
 65:         $this->value = $value === NULL ? NULL : key(array((string) $value => NULL));
 66:         return $this;
 67:     }
 68: 
 69: 
 70:     /**
 71:      * Returns selected key.
 72:      * @return scalar
 73:      */
 74:     public function getValue()
 75:     {
 76:         return array_key_exists($this->value, $this->items) ? $this->value : NULL;
 77:     }
 78: 
 79: 
 80:     /**
 81:      * Returns selected key (not checked).
 82:      * @return scalar
 83:      */
 84:     public function getRawValue()
 85:     {
 86:         return $this->value;
 87:     }
 88: 
 89: 
 90:     /**
 91:      * Is any item selected?
 92:      * @return bool
 93:      */
 94:     public function isFilled()
 95:     {
 96:         return $this->getValue() !== NULL;
 97:     }
 98: 
 99: 
100:     /**
101:      * Sets items from which to choose.
102:      * @param  array
103:      * @param  bool
104:      * @return self
105:      */
106:     public function setItems(array $items, $useKeys = TRUE)
107:     {
108:         $this->items = $useKeys ? $items : array_combine($items, $items);
109:         return $this;
110:     }
111: 
112: 
113:     /**
114:      * Returns items from which to choose.
115:      * @return array
116:      */
117:     public function getItems()
118:     {
119:         return $this->items;
120:     }
121: 
122: 
123:     /**
124:      * Returns selected value.
125:      * @return mixed
126:      */
127:     public function getSelectedItem()
128:     {
129:         $value = $this->getValue();
130:         return $value === NULL ? NULL : $this->items[$value];
131:     }
132: 
133: 
134:     /**
135:      * Disables or enables control or items.
136:      * @param  bool|array
137:      * @return self
138:      */
139:     public function setDisabled($value = TRUE)
140:     {
141:         if (!is_array($value)) {
142:             return parent::setDisabled($value);
143:         }
144: 
145:         parent::setDisabled(FALSE);
146:         $this->disabled = array_fill_keys($value, TRUE);
147:         if (isset($this->disabled[$this->value])) {
148:             $this->value = NULL;
149:         }
150:         return $this;
151:     }
152: 
153: }
154: 
Nette 2.1 API documentation generated by ApiGen 2.8.0