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
  • 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\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(Nette\Forms\Form::BLANK)
 36:             ->addRule([$this, 'isOk'], Nette\Forms\Validator::$messages[self::VALID]);
 37:     }
 38: 
 39: 
 40:     /**
 41:      * Sets first prompt item in select box.
 42:      * @param  string|object
 43:      * @return static
 44:      */
 45:     public function setPrompt($prompt)
 46:     {
 47:         $this->prompt = $prompt;
 48:         return $this;
 49:     }
 50: 
 51: 
 52:     /**
 53:      * Returns first prompt item?
 54:      * @return mixed
 55:      */
 56:     public function getPrompt()
 57:     {
 58:         return $this->prompt;
 59:     }
 60: 
 61: 
 62:     /**
 63:      * Sets options and option groups from which to choose.
 64:      * @return static
 65:      */
 66:     public function setItems(array $items, $useKeys = true)
 67:     {
 68:         if (!$useKeys) {
 69:             $res = [];
 70:             foreach ($items as $key => $value) {
 71:                 unset($items[$key]);
 72:                 if (is_array($value)) {
 73:                     foreach ($value as $val) {
 74:                         $res[$key][(string) $val] = $val;
 75:                     }
 76:                 } else {
 77:                     $res[(string) $value] = $value;
 78:                 }
 79:             }
 80:             $items = $res;
 81:         }
 82:         $this->options = $items;
 83:         return parent::setItems(Nette\Utils\Arrays::flatten($items, true));
 84:     }
 85: 
 86: 
 87:     /**
 88:      * Generates control's HTML element.
 89:      * @return Nette\Utils\Html
 90:      */
 91:     public function getControl()
 92:     {
 93:         $items = $this->prompt === false ? [] : ['' => $this->translate($this->prompt)];
 94:         foreach ($this->options as $key => $value) {
 95:             $items[is_array($value) ? $this->translate($key) : $key] = $this->translate($value);
 96:         }
 97: 
 98:         return Nette\Forms\Helpers::createSelectBox(
 99:             $items,
100:             [
101:                 'disabled:' => is_array($this->disabled) ? $this->disabled : null,
102:             ] + $this->optionAttributes,
103:             $this->value
104:         )->addAttributes(parent::getControl()->attrs);
105:     }
106: 
107: 
108:     /**
109:      * @return static
110:      */
111:     public function addOptionAttributes(array $attributes)
112:     {
113:         $this->optionAttributes = $attributes + $this->optionAttributes;
114:         return $this;
115:     }
116: 
117: 
118:     /**
119:      * @return bool
120:      */
121:     public function isOk()
122:     {
123:         return $this->isDisabled()
124:             || $this->prompt !== false
125:             || $this->getValue() !== null
126:             || !$this->options
127:             || $this->control->size > 1;
128:     }
129: }
130: 
Nette 2.4-20180918 API API documentation generated by ApiGen 2.8.0