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:  * Select box control that allows single item selection.
 15:  *
 16:  * @author     David Grudl
 17:  *
 18:  * @property   bool $prompt
 19:  */
 20: class SelectBox extends ChoiceControl
 21: {
 22:     /** validation rule */
 23:     const VALID = ':selectBoxValid';
 24: 
 25:     /** @var array of option / optgroup */
 26:     private $options = array();
 27: 
 28:     /** @var mixed */
 29:     private $prompt = FALSE;
 30: 
 31: 
 32:     /**
 33:      * Sets first prompt item in select box.
 34:      * @param  string
 35:      * @return self
 36:      */
 37:     public function setPrompt($prompt)
 38:     {
 39:         if ($prompt === TRUE) { // back compatibility
 40:             trigger_error(__METHOD__ . '(TRUE) is deprecated; argument must be string.', E_USER_DEPRECATED);
 41:             $items = $this->getItems();
 42:             $prompt = reset($items);
 43:             unset($this->options[key($items)], $items[key($items)]);
 44:             $this->setItems($items);
 45:         }
 46:         $this->prompt = $prompt;
 47:         return $this;
 48:     }
 49: 
 50: 
 51:     /**
 52:      * Returns first prompt item?
 53:      * @return mixed
 54:      */
 55:     public function getPrompt()
 56:     {
 57:         return $this->prompt;
 58:     }
 59: 
 60: 
 61:     /**
 62:      * Sets options and option groups from which to choose.
 63:      * @return self
 64:      */
 65:     public function setItems(array $items, $useKeys = TRUE)
 66:     {
 67:         if (!$useKeys) {
 68:             $res = array();
 69:             foreach ($items as $key => $value) {
 70:                 unset($items[$key]);
 71:                 if (is_array($value)) {
 72:                     foreach ($value as $val) {
 73:                         $res[$key][(string) $val] = $val;
 74:                     }
 75:                 } else {
 76:                     $res[(string) $value] = $value;
 77:                 }
 78:             }
 79:             $items = $res;
 80:         }
 81:         $this->options = $items;
 82:         return parent::setItems(Nette\Utils\Arrays::flatten($items, TRUE));
 83:     }
 84: 
 85: 
 86:     /**
 87:      * Generates control's HTML element.
 88:      * @return Nette\Utils\Html
 89:      */
 90:     public function getControl()
 91:     {
 92:         $items = $this->prompt === FALSE ? array() : array('' => $this->translate($this->prompt));
 93:         foreach ($this->options as $key => $value) {
 94:             $items[is_array($value) ? $this->translate($key) : $key] = $this->translate($value);
 95:         }
 96: 
 97:         return Nette\Forms\Helpers::createSelectBox(
 98:             $items,
 99:             array(
100:                 'selected?' => $this->value,
101:                 'disabled:' => is_array($this->disabled) ? $this->disabled : NULL
102:             )
103:         )->addAttributes(parent::getControl()->attrs);
104:     }
105: 
106: 
107:     /**
108:      * Performs the server side validation.
109:      * @return void
110:      */
111:     public function validate()
112:     {
113:         parent::validate();
114:         if (!$this->isDisabled() && $this->prompt === FALSE && $this->getValue() === NULL && $this->options) {
115:             $this->addError(Nette\Forms\Rules::$defaultMessages[self::VALID]);
116:         }
117:     }
118: 
119: }
120: 
Nette 2.1 API documentation generated by ApiGen 2.8.0