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

  • BaseControl
  • Button
  • Checkbox
  • CheckboxList
  • ChoiceControl
  • CsrfProtection
  • HiddenField
  • ImageButton
  • MultiChoiceControl
  • MultiSelectBox
  • RadioList
  • SelectBox
  • SubmitButton
  • TextArea
  • TextBase
  • TextInput
  • UploadControl
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  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:  * Select box control that allows single item selection.
 15:  */
 16: class SelectBox extends ChoiceControl
 17: {
 18:     /** validation rule */
 19:     const VALID = ':selectBoxValid';
 20: 
 21:     /** @var array of option / optgroup */
 22:     private $options = [];
 23: 
 24:     /** @var mixed */
 25:     private $prompt = false;
 26: 
 27:     /** @var array */
 28:     private $optionAttributes = [];
 29: 
 30: 
 31:     public function __construct($label = null, array $items = null)
 32:     {
 33:         parent::__construct($label, $items);
 34:         $this->setOption('type', 'select');
 35:         $this->addCondition(function () {
 36:             return $this->prompt === false
 37:                 && $this->options
 38:                 && $this->control->size < 2;
 39:         })->addRule(Nette\Forms\Form::FILLED, Nette\Forms\Validator::$messages[self::VALID]);
 40:     }
 41: 
 42: 
 43:     /**
 44:      * Sets first prompt item in select box.
 45:      * @param  string|object
 46:      * @return static
 47:      */
 48:     public function setPrompt($prompt)
 49:     {
 50:         $this->prompt = $prompt;
 51:         return $this;
 52:     }
 53: 
 54: 
 55:     /**
 56:      * Returns first prompt item?
 57:      * @return mixed
 58:      */
 59:     public function getPrompt()
 60:     {
 61:         return $this->prompt;
 62:     }
 63: 
 64: 
 65:     /**
 66:      * Sets options and option groups from which to choose.
 67:      * @return static
 68:      */
 69:     public function setItems(array $items, $useKeys = true)
 70:     {
 71:         if (!$useKeys) {
 72:             $res = [];
 73:             foreach ($items as $key => $value) {
 74:                 unset($items[$key]);
 75:                 if (is_array($value)) {
 76:                     foreach ($value as $val) {
 77:                         $res[$key][(string) $val] = $val;
 78:                     }
 79:                 } else {
 80:                     $res[(string) $value] = $value;
 81:                 }
 82:             }
 83:             $items = $res;
 84:         }
 85:         $this->options = $items;
 86:         return parent::setItems(Nette\Utils\Arrays::flatten($items, true));
 87:     }
 88: 
 89: 
 90:     /**
 91:      * Generates control's HTML element.
 92:      * @return Nette\Utils\Html
 93:      */
 94:     public function getControl()
 95:     {
 96:         $items = $this->prompt === false ? [] : ['' => $this->translate($this->prompt)];
 97:         foreach ($this->options as $key => $value) {
 98:             $items[is_array($value) ? $this->translate($key) : $key] = $this->translate($value);
 99:         }
100: 
101:         return Nette\Forms\Helpers::createSelectBox(
102:             $items,
103:             [
104:                 'disabled:' => is_array($this->disabled) ? $this->disabled : null,
105:             ] + $this->optionAttributes,
106:             $this->value
107:         )->addAttributes(parent::getControl()->attrs);
108:     }
109: 
110: 
111:     /**
112:      * @return static
113:      */
114:     public function addOptionAttributes(array $attributes)
115:     {
116:         $this->optionAttributes = $attributes + $this->optionAttributes;
117:         return $this;
118:     }
119: 
120: 
121:     /**
122:      * @return bool
123:      */
124:     public function isOk()
125:     {
126:         return $this->isDisabled()
127:             || $this->prompt !== false
128:             || $this->getValue() !== null
129:             || !$this->options
130:             || $this->control->size > 1;
131:     }
132: }
133: 
Nette 2.4-20191120 API API documentation generated by ApiGen 2.8.0