Namespaces

  • Nette
    • Application
      • Diagnostics
      • Responses
      • Routers
      • UI
    • Caching
      • Storages
    • ComponentModel
    • Config
      • Adapters
      • Extensions
    • Database
      • Diagnostics
      • Drivers
      • Reflection
      • Table
    • DI
      • Diagnostics
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
      • Macros
    • Loaders
    • Localization
    • Mail
    • Reflection
    • Security
      • Diagnostics
    • Templating
    • Utils
      • PhpGenerator
  • NetteModule
  • None
  • PHP

Classes

  • BaseControl
  • Button
  • Checkbox
  • HiddenField
  • ImageButton
  • MultiSelectBox
  • RadioList
  • SelectBox
  • SubmitButton
  • TextArea
  • TextBase
  • TextInput
  • UploadControl
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (http://nette.org)
  5:  *
  6:  * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
  7:  *
  8:  * For the full copyright and license information, please view
  9:  * the file license.txt that was distributed with this source code.
 10:  */
 11: 
 12: namespace Nette\Forms\Controls;
 13: 
 14: use Nette;
 15: 
 16: 
 17: 
 18: /**
 19:  * Select box control that allows multiple item selection.
 20:  *
 21:  * @author     David Grudl
 22:  */
 23: class MultiSelectBox extends SelectBox
 24: {
 25: 
 26: 
 27:     /**
 28:      * Returns selected keys.
 29:      * @return array
 30:      */
 31:     public function getValue()
 32:     {
 33:         $allowed = array_keys($this->allowed);
 34:         if ($this->getPrompt()) {
 35:             unset($allowed[0]);
 36:         }
 37:         return array_intersect($this->getRawValue(), $allowed);
 38:     }
 39: 
 40: 
 41: 
 42:     /**
 43:      * Returns selected keys (not checked).
 44:      * @return array
 45:      */
 46:     public function getRawValue()
 47:     {
 48:         if (is_scalar($this->value)) {
 49:             $value = array($this->value);
 50: 
 51:         } elseif (!is_array($this->value)) {
 52:             $value = array();
 53: 
 54:         } else {
 55:             $value = $this->value;
 56:         }
 57: 
 58:         $res = array();
 59:         foreach ($value as $val) {
 60:             if (is_scalar($val)) {
 61:                 $res[] = $val;
 62:             }
 63:         }
 64:         return $res;
 65:     }
 66: 
 67: 
 68: 
 69:     /**
 70:      * Returns selected values.
 71:      * @return array
 72:      */
 73:     public function getSelectedItem()
 74:     {
 75:         if (!$this->areKeysUsed()) {
 76:             return $this->getValue();
 77: 
 78:         } else {
 79:             $res = array();
 80:             foreach ($this->getValue() as $value) {
 81:                 $res[$value] = $this->allowed[$value];
 82:             }
 83:             return $res;
 84:         }
 85:     }
 86: 
 87: 
 88: 
 89:     /**
 90:      * Returns HTML name of control.
 91:      * @return string
 92:      */
 93:     public function getHtmlName()
 94:     {
 95:         return parent::getHtmlName() . '[]';
 96:     }
 97: 
 98: 
 99: 
100:     /**
101:      * Generates control's HTML element.
102:      * @return Nette\Utils\Html
103:      */
104:     public function getControl()
105:     {
106:         $control = parent::getControl();
107:         $control->multiple = TRUE;
108:         return $control;
109:     }
110: 
111: 
112: 
113:     /**
114:      * Count/length validator.
115:      * @param  MultiSelectBox
116:      * @param  array  min and max length pair
117:      * @return bool
118:      */
119:     public static function validateLength(MultiSelectBox $control, $range)
120:     {
121:         if (!is_array($range)) {
122:             $range = array($range, $range);
123:         }
124:         $count = count($control->getSelectedItem());
125:         return ($range[0] === NULL || $count >= $range[0]) && ($range[1] === NULL || $count <= $range[1]);
126:     }
127: 
128: }
129: 
Nette Framework 2.0.0 API API documentation generated by ApiGen 2.7.0