4
* This file is part of the Nette Framework (http://nette.org)
6
* Copyright (c) 2004 David Grudl (http://davidgrudl.com)
8
* For the full copyright and license information, please view
9
* the file license.txt that was distributed with this source code.
12
namespace Nette\Forms\Controls;
18
* Select box control that allows single item selection.
22
* @property-read mixed $rawValue
23
* @property bool $prompt
24
* @property array $items
25
* @property-read string $selectedItem
27
class SelectBox extends BaseControl
30
private $items = array();
33
protected $allowed = array();
36
private $prompt = FALSE;
39
private $useKeys = TRUE;
44
* @param array items from which to choose
45
* @param int number of rows that should be visible
47
public function __construct($label = NULL, array $items = NULL, $size = NULL)
49
parent::__construct($label);
50
$this->control->setName('select');
51
$this->control->size = $size > 1 ? (int) $size : NULL;
52
if ($items !== NULL) {
53
$this->setItems($items);
59
* Returns selected item key.
62
public function getValue()
64
return is_scalar($this->value) && isset($this->allowed[$this->value]) ? $this->value : NULL;
69
* Returns selected item key (not checked).
72
public function getRawValue()
74
return is_scalar($this->value) ? $this->value : NULL;
79
* Has been any item selected?
82
public function isFilled()
84
$value = $this->getValue();
85
return is_array($value) ? count($value) > 0 : $value !== NULL;
90
* Sets first prompt item in select box.
94
public function setPrompt($prompt)
96
if ($prompt === TRUE) { // back compatibility
97
$prompt = reset($this->items);
98
unset($this->allowed[key($this->items)], $this->items[key($this->items)]);
100
$this->prompt = $prompt;
106
function skipFirst($v = NULL)
108
trigger_error(__METHOD__ . '() is deprecated; use setPrompt() instead.', E_USER_WARNING);
109
return $this->setPrompt($v);
114
* Returns first prompt item?
117
final public function getPrompt()
119
return $this->prompt;
127
final public function areKeysUsed()
129
return $this->useKeys;
134
* Sets items from which to choose.
139
public function setItems(array $items, $useKeys = TRUE)
142
foreach ($items as $k => $v) {
143
foreach ((is_array($v) ? $v : array($k => $v)) as $key => $value) {
145
if (!is_scalar($value)) {
146
throw new Nette\InvalidArgumentException("All items must be scalar.");
151
if (isset($allowed[$key])) {
152
throw new Nette\InvalidArgumentException("Items contain duplication for key '$key'.");
155
$allowed[$key] = $value;
159
$this->items = $items;
160
$this->allowed = $allowed;
161
$this->useKeys = (bool) $useKeys;
167
* Returns items from which to choose.
170
final public function getItems()
177
* Returns selected value.
180
public function getSelectedItem()
182
$value = $this->getValue();
183
return ($this->useKeys && $value !== NULL) ? $this->allowed[$value] : $value;
188
* Generates control's HTML element.
189
* @return Nette\Utils\Html
191
public function getControl()
193
$selected = $this->getValue();
194
$selected = is_array($selected) ? array_flip($selected) : array($selected => TRUE);
195
$control = parent::getControl();
196
$option = Nette\Utils\Html::el('option');
198
if ($this->prompt !== FALSE) {
199
$control->add($this->prompt instanceof Nette\Utils\Html
200
? $this->prompt->value('')
201
: (string) $option->value('')->setText($this->translate((string) $this->prompt))
205
foreach ($this->items as $key => $value) {
206
if (!is_array($value)) {
207
$value = array($key => $value);
210
$dest = $control->create('optgroup')->label($this->translate($key));
213
foreach ($value as $key2 => $value2) {
214
if ($value2 instanceof Nette\Utils\Html) {
215
$dest->add((string) $value2->value($key2)
216
->selected(isset($selected[$key2])));
219
$key2 = $this->useKeys ? $key2 : $value2;
220
$dest->add((string) $option->value($key2)
221
->selected(isset($selected[$key2]))
222
->setText($this->translate((string) $value2)));