Namespaces

  • Latte
    • Loaders
    • Macros
    • Runtime
  • Nette
    • Application
      • Responses
      • Routers
      • UI
    • Bridges
      • ApplicationLatte
      • ApplicationTracy
      • CacheLatte
      • DatabaseDI
      • DatabaseTracy
      • DITracy
      • FormsLatte
      • Framework
      • HttpTracy
      • SecurityTracy
    • Caching
      • Storages
    • ComponentModel
    • Database
      • Drivers
      • Reflection
      • Table
    • DI
      • Config
        • Adapters
      • Extensions
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
    • Loaders
    • Localization
    • Mail
    • Neon
    • PhpGenerator
    • Reflection
    • Security
    • Templating
    • Utils
  • NetteModule
  • none
  • Tracy

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