Source for file Select.php
Documentation is available at Select.php
* Classes for <select> elements
* Copyright (c) 2006, 2007, Alexey Borzov <avb@php.net>,
* Bertrand Mansion <golgote@mamasam.com>
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * The names of the authors may not be used to endorse or promote products
* derived from this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* @package HTML_QuickForm2
* @author Alexey Borzov <avb@php.net>
* @author Bertrand Mansion <golgote@mamasam.com>
* @license http://opensource.org/licenses/bsd-license.php New BSD License
* @version CVS: $Id: Select.php,v 1.11 2007/06/30 20:36:00 avb Exp $
* @link http://pear.php.net/package/HTML_QuickForm2
* Base class for simple HTML_QuickForm2 elements
require_once 'HTML/QuickForm2/Element.php';
* Collection of <option>s and <optgroup>s
* This class handles the output of <option> tags. The class is not intended to
* @package HTML_QuickForm2
* @author Alexey Borzov <avb@php.net>
* @author Bertrand Mansion <golgote@mamasam.com>
* @version Release: 0.2.0
implements IteratorAggregate , Countable
* List of options and optgroups in this container
* Options are stored as arrays (for performance reasons), optgroups as
* instances of Optgroup class.
* Reference to parent <select>'s values
* Reference to parent <select>'s possible values
* @param array Reference to values of parent <select> element
* @param array Reference to possible values of parent <select> element
public function __construct(&$values, &$possibleValues)
* Please note that if you pass 'selected' attribute in the $attributes
* parameter then this option's value will be added to <select>'s values.
* @param string Option text
* @param string 'value' attribute for <option> tag
* @param mixed Additional attributes for <option> tag (either as a
* string or as an associative array)
public function addOption($text, $value, $attributes = null )
if (null === $attributes) {
$attributes = array ('value' => (string) $value);
$attributes = self ::prepareAttributes ($attributes);
if (isset ($attributes['selected'])) {
// the 'selected' attribute will be set in __toString()
unset ($attributes['selected']);
$attributes['value'] = (string) $value;
if (!isset ($attributes['disabled'])) {
$this->options[] = array ('text' => $text, 'attr' => $attributes);
* @param string 'label' attribute for optgroup tag
* @param mixed Additional attributes for <optgroup> tag (either as a
* string or as an associative array)
* @return HTML_QuickForm2_Element_Select_Optgroup
public function addOptgroup($label, $attributes = null )
* Returns an array of contained options
$indentLvl = $this->getIndentLevel ();
$indent = $this->getIndent () . self ::getOption ('indent');
$linebreak = self ::getOption ('linebreak');
foreach ($this->options as $option) {
if (in_array($option['attr']['value'], $strValues, true )) {
$option['attr']['selected'] = 'selected';
$html .= $indent . '<option' .
self ::getAttributesString ($option['attr']) .
'>' . $option['text'] . '</option>' . $linebreak;
$option->setIndentLevel ($indentLvl + 1 );
$html .= $option->__toString ();
* Returns an iterator over contained elements
* @return HTML_QuickForm2_Element_Select_OptionIterator
* Returns a recursive iterator over contained elements
* @return RecursiveIteratorIterator
return new RecursiveIteratorIterator (
RecursiveIteratorIterator ::SELF_FIRST
* Returns the number of options in the container
* Class representing an <optgroup> tag
* Do not instantiate this class yourself, use
* {@link HTML_QuickForm2_Element_Select::addOptgroup()} method
* @package HTML_QuickForm2
* @author Alexey Borzov <avb@php.net>
* @author Bertrand Mansion <golgote@mamasam.com>
* @version Release: 0.2.0
* @param array Reference to values of parent <select> element
* @param array Reference to possible values of parent <select> element
* @param string 'label' attribute for optgroup tag
* @param mixed Additional attributes for <optgroup> tag (either as a
* string or as an associative array)
public function __construct(&$values, &$possibleValues, $label, $attributes = null )
$this->setAttributes ($attributes);
$this->attributes['label'] = (string) $label;
$indent = $this->getIndent ();
$linebreak = self ::getOption ('linebreak');
return $indent . '<optgroup' . $this->getAttributes (true ) . '>' .
$linebreak . parent ::__toString() . $indent . '</optgroup>' . $linebreak;
* Implements a recursive iterator for options arrays
* @package HTML_QuickForm2
* @author Alexey Borzov <avb@php.net>
* @author Bertrand Mansion <golgote@mamasam.com>
* @version Release: 0.2.0
implements RecursiveIterator
$this->current ()->getOptions ()
* Class representing a <select> element
* @package HTML_QuickForm2
* @author Alexey Borzov <avb@php.net>
* @author Bertrand Mansion <golgote@mamasam.com>
* @version Release: 0.2.0
* Values for the select element (i.e. values of the selected options)
* Possible values for select elements
* A value is considered possible if it is present as a value attribute of
* some option and that option is not disabled.
* Object containing options for the <select> element
* @var HTML_QuickForm2_Element_Select_OptionContainer
* @param string Element name
* @param mixed Attributes (either a string or an array)
* @param array Data used to populate the element's options, passed to
* {@link loadOptions()} method. Format:
* $data = array('options' => array('option1', 'option2'));
* @throws HTML_QuickForm2_InvalidArgumentException if junk is given in $options
public function __construct($name = null , $attributes = null , array $data = array ())
$options = isset ($data['options'])? $data['options']: array ();
parent ::__construct ($name, $attributes, $data);
if (empty ($this->attributes['multiple'])) {
$attrString = $this->getAttributes (true );
$this->attributes['name'] .= '[]';
$attrString = $this->getAttributes (true );
$this->attributes['name'] = substr($this->attributes['name'], 0 , -2 );
$indent = $this->getIndent ();
return $indent . '<select' . $attrString . '>' .
self ::getOption ('linebreak') .
if (null === ($value = $this->getValue())) {
if (is_array($child) && isset ($valueHash[$child['attr']['value']]) &&
empty ($child['attr']['disabled']))
$options[] = $child['text'];
$html = implode('<br />', $options);
$name = $this->attributes['name'] .
(empty ($this->attributes['multiple'])? '': '[]');
// Only use id attribute if doing single hidden input
$idAttr = (1 == count($valueHash))? array ('id' => $this->getId()): array ();
foreach ($valueHash as $key => $item) {
$html .= '<input type="hidden"' . self ::getAttributesString (array (
* Returns the value of the <select> element
* Please note that the returned value may not necessarily be equal to that
* passed to {@link setValue()}. It passes "intrinsic validation" confirming
* that such value could possibly be submitted by this <select> element.
* Specifically, this method will return null if the elements "disabled"
* attribute is set, it will not return values if there are no options having
* such a "value" attribute or if such options' "disabled" attribute is set.
* It will also only return a scalar value for single selects, mimicking
* the common browsers' behaviour.
* @return mixed "value" attribute of selected option in case of single
* select, array of selected options' "value" attributes in
* case of multiple selects, null if no options selected
foreach ($this->values as $value) {
if (0 == count($values)) {
} elseif (!empty ($this->attributes['multiple'])) {
} elseif (1 == count($values)) {
// The <select> is not multiple, but several options are to be
// selected. At least IE and Mozilla select the last selected
// option in this case, we should do the same
$lastValue = $child['attr']['value'];
$this->values = array ($value);
* Loads <option>s (and <optgroup>s) for select element
* The method expects a array of options and optgroups:
* 'option value 1' => 'option text 1',
* 'option value N' => 'option text N',
* 'optgroup label 1' => array(
* 'option value' => 'option text',
* If value is a scalar, then array key is treated as "value" attribute of
* <option> and value as this <option>'s text. If value is an array, then
* key is treated as a "label" attribute of <optgroup> and value as an
* array of <option>s for this <optgroup>.
* If you need to specify additional attributes for <option> and <optgroup>
* tags, then you need to use {@link addOption()} and {@link addOptgroup()}
* methods instead of this one.
* @throws HTML_QuickForm2_InvalidArgumentException if junk is given in $options
* @return HTML_QuickForm2_Element_Select
$this->possibleValues = array ();
$this->optionContainer = new HTML_QuickForm2_Element_Select_OptionContainer (
$this->values , $this->possibleValues
* Adds options from given array into given container
* @param HTML_QuickForm2_Element_Select_OptionContainer options will be
* added to this container
* @param array options array
HTML_QuickForm2_Element_Select_OptionContainer $container, $options
foreach ($options as $key => $value) {
* Please note that if you pass 'selected' attribute in the $attributes
* parameter then this option's value will be added to <select>'s values.
* @param string Option text
* @param string 'value' attribute for <option> tag
* @param mixed Additional attributes for <option> tag (either as a
* string or as an associative array)
public function addOption($text, $value, $attributes = null )
* @param string 'label' attribute for optgroup tag
* @param mixed Additional attributes for <optgroup> tag (either as a
* string or as an associative array)
* @return HTML_QuickForm2_Element_Select_Optgroup
public function addOptgroup($label, $attributes = null )
if (!$this->getAttribute ('multiple')) {
if (null !== ($value = $ds->getValue($name)) ||
$this->setValue(null === $value? array (): $value);
Documentation generated on Mon, 22 Oct 2007 12:30:24 -0400 by phpDocumentor 1.4.0. PEAR Logo Copyright © PHP Group 2004.
|