cvs: pear /HTML_QuickForm_altselect altselect.php package.xml /HTML_QuickForm_altselect/docs/examples example.php /HTML_QuickForm_altselect/tests
01.phpt 02.phpt 03.phpt 04.phpt 05.phpt 06.phpt 07.phpt 08.phpt 09.phpt 10.phpt 11.phpt 12.phpt 13.phpt 14.phpt 15.phpt

From: Date: Tue, 13 Feb 2007 07:51:04 +0000
Subject: cvs: pear /HTML_QuickForm_altselect altselect.php package.xml /HTML_QuickForm_altselect/docs/examples example.php /HTML_QuickForm_altselect/tests
01.phpt 02.phpt 03.phpt 04.phpt 05.phpt 06.phpt 07.phpt 08.phpt 09.phpt 10.phpt 11.phpt 12.phpt 13.phpt 14.phpt 15.phpt
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-44649@lists.php.net to get a copy of this message
shangxiao Tue Feb 13 07:51:04 2007 UTC Added files: /pear/HTML_QuickForm_altselect altselect.php package.xml /pear/HTML_QuickForm_altselect/docs/examples example.php /pear/HTML_QuickForm_altselect/tests 01.phpt 02.phpt 03.phpt 04.phpt 05.phpt 06.phpt 07.phpt 08.phpt 09.phpt 10.phpt 11.phpt 12.phpt 13.phpt 14.phpt 15.phpt Log:

http://cvs.php.net/viewvc.cgi/pear/HTML_QuickForm_altselect/altselect.php?view=markup&rev=1.1 Index: pear/HTML_QuickForm_altselect/altselect.php +++ pear/HTML_QuickForm_altselect/altselect.php <?php /* vim: set expandtab tabstop=4 shiftwidth=4: */ /** * HTML QuickForm Alternate Select * * This file must be included *after* HTML/QuickForm.php * * HTML_QuickForm plugin that changes a select into a group of radio buttons * or checkboxes with an optional textbox for other options not listed. If * the select element is listed as multiple, then it will be rendered with * checkboxes, otherwise it is rendered with radio buttons. * * PHP Versions 4 and 5 * * @category HTML * @package HTML_QuickForm_altselect * @author David Sanders (shang.xiao.sanders@gmail.com) * @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1 * @version Release: @package_version@ * @link http://pyrus.sourceforge.net/HTML_QuickForm_altselect.html * @see HTML_QuickForm_select */ require_once 'HTML/QuickForm/select.php'; /** * Replace PHP_EOL constant * * category PHP * package PHP_Compat * @link http://php.net/reserved.constants.core * @author Aidan Lister <aidan@php.net> * @since PHP 5.0.2 */ if (!defined('PHP_EOL')) { switch (strtoupper(substr(PHP_OS, 0, 3))) { // Windows case 'WIN': define('PHP_EOL', "\r\n"); break; // Mac case 'DAR': define('PHP_EOL', "\r"); break; // Unix default: define('PHP_EOL', "\n"); } } // {{{ HTML_QuickForm_altselect /** * HTML QuickForm Alternate Select * * HTML_QuickForm plugin that changes a select into a group of radio buttons * or checkboxes with an optional textbox for other options not listed. If * the select element is listed as multiple, then it will be rendered with * checkboxes, otherwise it is rendered with radio buttons. * * @category HTML * @package HTML_QuickForm_altselect * @author David Sanders (shang.xiao.sanders@gmail.com) * @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1 * @version Release: @package_version@ * @link http://pyrus.sourceforge.net/HTML_QuickForm_altselect.html * @see HTML_QuickForm_select */ class HTML_QuickForm_altselect extends HTML_QuickForm_select { // {{{ properties /** * Include the other text field for non-listed entry. * * @var bool * @access public */ var $includeOther = false; /** * Label for the Other option. * * @var string * @access public */ var $otherLabel = 'Other'; /** * Text label to go in front of other text field (singular mode). * * @var string * @access public */ var $otherText = 'If other please specify:'; /** * Text label to go in front of other text field (multiple mode). * * @var string * @access public */ var $otherTextMultiple = 'Other:'; /** * Delimiter between subelements. Use br to go vertical, or nbsp to go * horizontal. * * @var string * @access public */ var $delimiter = '<br />'; /** * Other value storage. * * @var string * @access private */ var $_otherValue; /** * Associative array of attributes for each of the individual form elements. * NOTE: use "_qf_other" for the other radio button, and "_qf_other_text" * for the text field. * * @var array Associative array of attributes (see HTML_Common) * @access private */ var $_individualAttributes; // }}} // {{{ HTML_QuickForm_altselect /** * Constructor. Used to distinguish the attributes array which should be * an associative array of options to either a typical HTML attribute string * or another associative array * * @param string $elementName select name attribute * @param mixed $elementLabel label(s) for the select * @param mixed $options data to be used to populate options * @param mixed $attributes an associative array of option value * -> attributes. Each attribute is either * a typical HTML attribute string or an * associative array. * NOTE: use "_qf_other" for the other radio * button, and "_qf_other_text" for the * text field. * @return void */ function HTML_QuickForm_altselect($elementName = null, $elementLabel = null, $options = null, $attributes = null) { if (func_get_args()) { HTML_QuickForm_select::HTML_QuickForm_select($elementName, $elementLabel, $options); $this->_individualAttributes = $attributes; } } // }}} // {{{ toHtml /** * Render the HTML_QuickForm element. * * @access public * @return string The rendered HTML */ function toHtml() { return $this->getElements(false); } // }}} // {{{ getElements /** * Arrange the buttons/boxes and other bits either concatenated as a html * string or in an array. When this element is registered as a group, * getElements should act in the same way as HTML_QuickForm_group::getElements(). * (Therefore the default must be to format as an array) * * @param bool $formatArray set true for an array (default), false for HTML * @access public * @see HTML_QuickForm_group::getElements() * @return mixed Array or HTML string */ function getElements($formatArray = true) { $html_func_to_use = $this->_flagFrozen ? 'getFrozenHtml' : 'toHtml'; $is_multiple = $this->getMultiple(); if ($formatArray) { $elements = array(); } else { $preHtml = ''; $postHtml = ''; $htmlArray = array(); $tabs = $this->_getTabs(); if ($this->getComment() != '') { $preHtml .= '<!-- ' . $this->getComment() . ' //-->' . PHP_EOL; } } if ($this->includeOther && !defined('HTML_QUICKFORM_ALTSELECT_JS_DISABLE_ELEMENT')) { $disable_element_js = <<<EOT <script type="text/javascript"> //<![CDATA[ function _qf_altselect_disableElement(e,disable) { if (!disable) { e.disabled = false; e.style.backgroundColor = '#ffffff'; } else { e.disabled = true; e.style.backgroundColor = '#cccccc'; } } //]]> </script> EOT; if ($formatArray && !$this->_flagFrozen) { $elements['_qf_altselect_disableElement'] =& HTML_QuickForm::createElement('static', '_qf_altselect_disableElement', null, $disable_element_js); } else { $javascript = $disable_element_js; } define('HTML_QUICKFORM_ALTSELECT_JS_DISABLE_ELEMENT',true); } else { $javascript = ''; } $myName = $this->getName(); if ($is_multiple) { $myName .= '[]'; } foreach ($this->_options as $option) { if ($is_multiple) { $element =& HTML_QuickForm::createElement('checkbox',$myName); //xxx - qf won't take a value as constructor argument $element->updateAttributes(array('value' => $option['attr']['value'])); } else { $element =& HTML_QuickForm::createElement('radio', $myName, null, null, $option['attr']['value']); if ($this->includeOther) { $element->updateAttributes(array( 'onClick' => "_qf_altselect_disableElement(this.form.elements[this.name + '_qf_other'],true);")); } } if (isset($this->_individualAttributes[$option['attr']['value']])) { $element->updateAttributes($this->_individualAttributes[$option['attr']['value']]); } if (is_array($this->_values) && in_array((string)$option['attr']['value'], $this->_values)) { $element->setChecked(true); } if ($formatArray) { if ($this->_flagFrozen) { $element->freeze(); } $elements[$option['attr']['value']] =& $element; } else { // write our own label instead of adding text to the radio/cbox // as we may want to render without any text when doing from a group $htmlArray[] = $tabs . $element->$html_func_to_use() . '<label for="' . $element->getAttribute('id') . '">' . $option['text'] . '</label>'; } } if ($this->includeOther) { if (!$is_multiple) { // // create the other radio button // $element =& HTML_QuickForm::createElement('radio', $myName, null, null, '_qf_other'); if (isset($this->_individualAttributes['_qf_other'])) { $element->updateAttributes($this->_individualAttributes['_qf_other']); } $element->updateAttributes(array( 'onClick'=>"_qf_altselect_disableElement(this.form.elements[this.name+'_qf_other'],false);this.form.elements[this.name+'_qf_other'].focus();this.form.elements[this.name+'_qf_other'].select();")); if (is_array($this->_values) && in_array('_qf_other', $this->_values)) { $element->setChecked(true); } $other_msg = $this->otherText; if ($formatArray) { if ($this->_flagFrozen) { $element->freeze(); } $elements['_qf_other'] =& $element; } else { $htmlArray[] = $tabs . $element->$html_func_to_use() . '<label for="' . $element->getAttribute('id') . '">' . $this->otherLabel . '</label>'; $preHtml .= $javascript; } $textName = $myName.'_qf_other'; } else { $other_msg = $this->otherTextMultiple; $textName = $myName; } // // create the 'other' text element // $other_id = 'qf_' . uniqid(''); $element =& HTML_QuickForm::createElement('text', $textName, null, array('id'=>$other_id)); if (isset($this->_individualAttributes['_qf_other_text'])) { $element->updateAttributes($this->_individualAttributes['_qf_other_text']); } // if either the other button is selected, or some text is entered // (meaning _qf_other will also be a value), then set the value of // the other value in the text field. if (is_array($this->_values) && in_array('_qf_other', $this->_values) && isset($this->_otherValue)) { $element->updateAttributes(array('value' => $this->_otherValue)); } // otherwise just disable it // only disable with javascript otherwise if the browser doesn't have // javascript, then we'll never be able to enter the other text else if (!$is_multiple) { $disable_js = <<<EOT <script type="text/javascript"> //<![CDATA[ _qf_altselect_disableElement(document.getElementById('$other_id'),true); //]]> </script> EOT; if ($formatArray && !$this->_flagFrozen) { $elements['disable_js'] =& HTML_QuickForm::createElement('static', 'disable_js', null, $disable_js); } else { $postHtml .= $disable_js; } } if ($formatArray) { if ($this->_flagFrozen) { $element->freeze(); } $elements[$textName] =& $element; } else { $htmlArray[] = $tabs . '<label for="' . $element->getAttribute('id') . '">' . $other_msg . '</label> ' . $element->$html_func_to_use(); } } if ($formatArray) { return $elements; } else { $strHtml = $preHtml . PHP_EOL . implode($this->delimiter . PHP_EOL, $htmlArray) . PHP_EOL . $postHtml; return $strHtml; } } // }}} // {{{ exportValue /** * Exports the value. * * If the other value is set, this will be exported if in singular mode * and the other radio button is selected. Otherwise if in multiple mode * the other value is added to the array of values. * * @param array $submitValues submitValues values submitted * @param bool $assoc propagate on to * HTML_QuickForm_select::exportValue() * @access public * @return mixed Single value or array of values */ function exportValue(&$submitValues, $assoc = false) { if (!$this->includeOther) { return parent::exportValue($submitValues, $assoc); } if ($this->getMultiple()) { // kinda defeats the purpose of using exportValue to return only // allowed options $value = $this->_findValue($submitValues); // if nothing was posted, then grab the defaults if (is_null($value)) { $value = $this->getValue(); } // _findValue may return a scalar if that's what was posted if (is_array($value)) { // remove the empty string from the other option foreach ($value as $key => $item) { if ($item === '') { unset($value[$key]); } } } else { // if a scalar, force an array to be exported $value = array($value); } return $this->_prepareValue($value, $assoc); } else { // NB: If an array was submitted with an other value, then it will // not be exported, as we're expecting '_qf_other' to be submitted // in singular mode. $myName = $this->getName(); // XXX $this->addOption(null,'_qf_other'); $value = parent::exportValue($submitValues, $assoc); // XXX array_pop($this->_options); if (is_array($value) && $value[$myName] == '_qf_other' || $value == '_qf_other') { if (isset($submitValues[$myName.'_qf_other'])) { $value = $submitValues[$myName.'_qf_other']; } else { $value = $this->_otherValue; } } return $this->_prepareValue($value, $assoc); } } // }}} // {{{ setSelected /** * Set the selected options. If a non-listed option is specified, it * will go into the other text field. Note at this point, the other and * multiple attributes may not have been set. * * @param mixed $values array or comma delimited string of selected values * @access public * @return void */ function setSelected($values) { parent::setSelected($values); // // we need to do some extra work here in case the other // option will be/has been set... // $other_values = array(); foreach ($this->_values as $value) { // if we are in singular mode and the other button is selected from // the submit values then we'll need to record the real other value // in _otherValue if ($value == '_qf_other') { $myName = $this->getName(); $this->_otherValue = @$_REQUEST[$myName.'_qf_other']; // we only need to grasp the first other value, because we // are in singular mode, so we'll return... return; } // otherwise the real other value might be listed in _values // from setSelected('junk') or if we're in multiple mode and it was // submitted... // if we find something not part of the options then we record it in // _otherValue and set the _qf_other as part of the values else { $found = false; foreach ($this->_options as $option) { if ((string) $value == (string) $option['attr']['value']) { $found = true; } } if (!$found) { $this->_values[] = '_qf_other'; $other_values[] = $value; } } } if (!empty($other_values)) { $this->_otherValue = implode(',',$other_values); } } // }}} // {{{ setIncludeOther /** * Set the select to include the 'other' textfield. * * @param bool $include whether to include the other text field * @access public * @return void */ function setIncludeOther($include = true) { $this->includeOther = (bool) $include; } // }}} // {{{ setDelimiter /** * Set the delimiter. * * @param string $delimiter delimiter to use between the subelements * @access public * @return void */ function setDelimiter($delimiter) { if (!is_string($delimiter)) { $this->delimiter = '<br />'; } else { $this->delimiter = $delimiter; } } // }}} // {{{ setGroup /** * Tell this element to act like a group when being accepted. * * @param bool $is_group whether to act like a group or not * @see HTML_QuickForm_element::_type * @access public * @return void */ function setGroup($is_group = true) { $this->_type = $is_group ? 'group' : 'select'; } // }}} // {{{ accept /** * Accepts a renderer. Overload select in case we'd like to see the * checkboxes/radio buttons in a group when renderering with another * renderer. * * This function was copied from HTML_QuickForm_group::accept() and * modified for use with this class. * * @param HTML_QuickForm_Renderer $renderer the QF renderer * @param bool $require whether a group is required * @param string $error an error message associated with a group * @see HTML_QuickForm_group::accept() * @access public * @return void */ function accept(&$renderer, $required = false, $error = null) { // if not asked to act like a group, then pass off to regular accept method if ($this->_type != 'group') { return parent::accept($renderer, $required, $error); } $this->_separator = null; $this->_appendName = null; $this->_required = array(); // Beginning of code from HTML_QuickForm_group::accept() // ---8<--- //$this->_createElementsIfNotExist(); $renderer->startGroup($this, $required, $error); $name = $this->getName(); // --->8--- // use our method to get the elements instead $this->_elements = $this->getElements(); // ---8<--- foreach (array_keys($this->_elements) as $key) { $element =& $this->_elements[$key]; if ($this->_appendName) { $elementName = $element->getName(); if (isset($elementName)) { $element->setName($name . '[' . (strlen($elementName)? $elementName: $key) . ']'); } else { $element->setName($name); } } $required = !$element->isFrozen() && in_array($element->getName(), $this->_required); $element->accept($renderer, $required); // restore the element's name if ($this->_appendName) { $element->setName($elementName); } } $renderer->finishGroup($this); // --->8--- } // }}} // {{{ getElementName /** * Returns the name of this element. Used by HTML_QuickForm::getSubmitValue() * when this element is registered as a group. * * @see HTML_QuickForm::getSubmitValue() * @access public * @return string */ function getElementName() { return $this->getName(); } // }}} } // }}} if (class_exists('HTML_QuickForm')) { HTML_QuickForm::registerElementType('altselect', 'HTML/QuickForm/altselect.php', 'HTML_QuickForm_altselect'); } /* * Local variables: * tab-width: 4 * c-basic-offset: 4 * c-hanging-comment-ender-p: nil * End: */ ?> http://cvs.php.net/viewvc.cgi/pear/HTML_QuickForm_altselect/package.xml?view=markup&rev=1.1 Index: pear/HTML_QuickForm_altselect/package.xml +++ pear/HTML_QuickForm_altselect/package.xml <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE package SYSTEM "http://pear.php.net/dtd/package-1.0"> <package version="1.0" packagerversion="1.4.5"> <name>HTML_QuickForm_altselect</name> <summary>A HTML_QuickForm element with options represented as either radio buttons or checkboxes and an optional 'other' text field.</summary> <description>A class that extends the HTML_QuickForm_select class to make a quickform element with options represented as either radio buttons or checkboxes depending on whether or not the multiple attribute was set. An 'other' textfield may also be added for discretionary alternatives.</description> <maintainers> <maintainer> <user>dsanders</user> <name>David Sanders</name> <email>shang.xiao.sanders@gmail.com</email> <role>lead</role> </maintainer> </maintainers> <release> <version>1.0.2</version> <date>2007-02-13</date> <license uri="http://www.gnu.org/licenses/lgpl.txt">LGPL</license> <state>stable</state> <notes>Fixed exportValue() so that it behaves more like HTML_QuickForm_select when dealing with nothing submitted, array submitted when singular and vice versa.</notes> <deps> <dep type="php" rel="ge" version="4.0.1" optional="no" /> <dep type="php" rel="ge" version="4.2" /> <dep type="pkg" rel="ge" version="1.2.1">HTML_Common</dep> <dep type="pkg" rel="ge" version="3.2.5">HTML_QuickForm</dep> </deps> <provides type="class" name="HTML_QuickForm_altselect" /> <filelist> <file role="php" baseinstalldir="HTML/QuickForm" md5sum="10df750da91f8bbda00b48a66545f309" name="altselect.php"> <replace from="@package_version@" to="version" type="package-info" /> </file> <dir role="test" name="tests"> <file md5sum="58f01cfbb59351cab30973d507c280e2" name="01.phpt" /> <file md5sum="a8ace73a364c22638a1fa40d3a4ec1ef" name="02.phpt" /> <file md5sum="5dbc7e962ad435a0bb0f6a7f0c87b068" name="03.phpt" /> <file md5sum="81f7a4525d11083f663c9fd589c802fb" name="04.phpt" /> <file md5sum="8b495b06357884a9d9e1e761d3937dd1" name="05.phpt" /> <file md5sum="8d451cc03a97b8417e87ea8aa929617c" name="06.phpt" /> <file md5sum="66c38f9d080f8fd0c8a4ddef20845ada" name="07.phpt" /> <file md5sum="a7af789f931ae41bb89a665552b384d7" name="08.phpt" /> <file md5sum="391bcda0c353f53eb46ea270cb49783f" name="09.phpt" /> <file md5sum="f7da6a630d857c0b0e2b628a171b90b8" name="10.phpt" /> <file md5sum="4e85aa053feb270375ba1d96591941ea" name="11.phpt" /> <file md5sum="f5eb100fb019051acb45ed67533751e6" name="12.phpt" /> <file md5sum="87c0a35a02053d19caa565972dde0260" name="13.phpt" /> <file md5sum="382a3999606c1bfe9ca628040d19cd76" name="14.phpt" /> <file md5sum="fc91c82e27c890f4dbaf442e026b7003" name="15.phpt" /> </dir> <dir role="doc" name="docs"> <dir name="examples"> <file md5sum="709af6087ef16ab4b024d33fbd9ede19" name="example.php" /> </dir> </dir> </filelist> </release> <changelog> <release> <version>1.0.1</version> <date>2007-02-02</date> <license uri="http://www.gnu.org/licenses/lgpl.txt">LGPL</license> <state>stable</state> <notes>- Change of license. - Added parent call in setSelected(). - Removed a few redundant checks. - HTML comment now displaying correctly. - Using tabs to align html, if they get set. - Fixed a php notice if the element was set for multiple and an array wasn't posted.</notes> </release> <release> <version>1.0.0</version> <date>2007-01-27</date> <license>PHP License</license> <state>stable</state> <notes>Fixed up coding standards according to PEAR</notes> </release> <release> <version>0.3.7</version> <date>2006-11-14</date> <license>BSD License</license> <state>beta</state> <notes>Fixed error message when export and nothing submitted for multiple select</notes> </release> <release> <version>0.3.6</version> <date>2006-10-11</date> <license>BSD License</license> <state>beta</state> <notes>- Fixed bug with freezing not being propagated correctly to individual elements when treating like a group.</notes> </release> <release> <version>0.3.5</version> <date>2006-10-10</date> <license>BSD License</license> <state>beta</state> <notes>- Use the QF function _findValue() to retrieve the element's value out of submitValues instead of direct access, in case the element name is using [] to specify that it is part of an array.</notes> </release> <release> <version>0.3.4</version> <date>2006-07-05</date> <license>BSD License</license> <state>beta</state> <notes>- Fixed exportValue so that it correctly exports the other value when assoc is true. Also uses _prepareValue to return a correct assoc array when returning a value. - Fixed setSelected so that it sets the other value always as the include_other option may not have been set by the time this function is called.</notes> </release> <release> <version>0.3.3</version> <date>2006-07-04</date> <license>BSD License</license> <state>beta</state> <notes>- Remove the _qf_other option from the option list after temporarily adding in exportValue. - Return from setSelected if a null value is passed.</notes> </release> <release> <version>0.3.2</version> <date>2006-05-29</date> <license>BSD License</license> <state>beta</state> <notes>- Export the array correctly now with the name of the element as the key. - Allowed multiple other options to be set in multiple mode.</notes> </release> <release> <version>0.3.1</version> <date>2006-05-26</date> <license>BSD License</license> <state>beta</state> <notes>- Renamed _getElements() to getElements() and added getElementName() to coincide with HTML_QuickForm_group for when registered as a group. - Only create the disable function when including the other textfield.</notes> </release> <release> <version>0.3.0</version> <date>2006-05-24</date> <license>BSD License</license> <state>beta</state> <notes>- Added ability to specifiy html attributes for each of the individual form elements. - Improved phpdoc commenting.</notes> </release> <release> <version>0.2.1</version> <date>2006-05-11</date> <license>BSD License</license> <state>beta</state> <notes>Added label tags for the element labels. Didn't include the labels as the text property of the radio or checkbox elements, as these may be required to render without the labels when rendering as a group in a template.</notes> </release> <release> <version>0.2.0</version> <date>2006-05-09</date> <license>BSD License</license> <state>beta</state> <notes>- Changed frozen html to use the individual element frozen html functions.</notes> </release> <release> <version>0.1.1</version> <date>2006-04-21</date> <license>BSD License</license> <state>alpha</state> <notes>- Added PHP_EOL constant. - Added HTML comment. - Fixed a few errors with rendering as a group.</notes> </release> <release> <version>0.1</version> <date>2006-03-03</date> <license>BSD License</license> <state>alpha</state> <notes>Initial checkin</notes> </release> </changelog> </package> http://cvs.php.net/viewvc.cgi/pear/HTML_QuickForm_altselect/docs/examples/example.php?view=markup&rev=1.1 Index: pear/HTML_QuickForm_altselect/docs/examples/example.php +++ pear/HTML_QuickForm_altselect/docs/examples/example.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> <title>HTML_QuickForm_altselect - Example Usage</title> </head> <body> <?php require_once 'HTML/QuickForm.php'; // Include altselect.php after QuickForm, so that the plugin can be loaded require_once 'HTML/QuickForm/altselect.php'; $form =& new HTML_QuickForm('test_altselect'); // Add the altselect element, using the same arguments as you would a normal select element $altselect =& $form->addElement('altselect', 'test', 'Select a value:', array('a' => 'A', 'b' => 'B', 'c' => 'C')); // Use the setMultiple() method, just as in HTML_QuickForm_select // to turn the radio buttons into checkboxes $altselect->setMultiple(true); // Use setIncludeOther() to include the 'other' textfield for other possible values $altselect->setIncludeOther(true); // Use setSelected(), just as in HTML_QuickForm_select // to set the default values $altselect->setSelected('b'); $form->addElement('submit', 'submit', 'Submit'); if ($form->validate()) { // The exported value will either be a scalar or an array depending on what was posted, // just as it would with HTML_QuickForm_select $value = $form->exportValue('test'); echo '<p>The result is: '.var_export($value, true).'</p>'; } $form->display(); ?> </body> </html> http://cvs.php.net/viewvc.cgi/pear/HTML_QuickForm_altselect/tests/01.phpt?view=markup&rev=1.1 Index: pear/HTML_QuickForm_altselect/tests/01.phpt +++ pear/HTML_QuickForm_altselect/tests/01.phpt --TEST-- 1.phpt: Test singular mode without other --FILE-- <?php require_once 'HTML/QuickForm.php'; require_once 'HTML/QuickForm/altselect.php'; $select =& HTML_QuickForm::createElement('altselect', 'test', null, array( 'a' => 'A', 'b' => 'B', 'c' => 'C', )); echo $select->toHtml(); ?> --EXPECTREGEX-- <input name=\"test\" value=\"a\" type=\"radio\" id=\"qf_([0-9a-zA-Z]+)\" \/><label for=\"qf_\1\">A<\/label><br \/> <input name=\"test\" value=\"b\" type=\"radio\" id=\"qf_([0-9a-zA-Z]+)\" \/><label for=\"qf_\2\">B<\/label><br \/> <input name=\"test\" value=\"c\" type=\"radio\" id=\"qf_([0-9a-zA-Z]+)\" \/><label for=\"qf_\3\">C<\/label> http://cvs.php.net/viewvc.cgi/pear/HTML_QuickForm_altselect/tests/02.phpt?view=markup&rev=1.1 Index: pear/HTML_QuickForm_altselect/tests/02.phpt +++ pear/HTML_QuickForm_altselect/tests/02.phpt --TEST-- 2.phpt: Test multiple mode without other --FILE-- <?php require_once 'HTML/QuickForm.php'; require_once 'HTML/QuickForm/altselect.php'; $select =& HTML_QuickForm::createElement('altselect', 'test', null, array( 'a' => 'A', 'b' => 'B', 'c' => 'C', )); $select->setMultiple(true); echo $select->toHtml(); ?> --EXPECTREGEX-- <input name=\"test\[\]\" type=\"checkbox\" value=\"a\" id=\"qf_([0-9a-zA-Z]+)\" \/><label for=\"qf_\1\">A<\/label><br \/> <input name=\"test\[\]\" type=\"checkbox\" value=\"b\" id=\"qf_([0-9a-zA-Z]+)\" \/><label for=\"qf_\2\">B<\/label><br \/> <input name=\"test\[\]\" type=\"checkbox\" value=\"c\" id=\"qf_([0-9a-zA-Z]+)\" \/><label for=\"qf_\3\">C<\/label> http://cvs.php.net/viewvc.cgi/pear/HTML_QuickForm_altselect/tests/03.phpt?view=markup&rev=1.1 Index: pear/HTML_QuickForm_altselect/tests/03.phpt +++ pear/HTML_QuickForm_altselect/tests/03.phpt --TEST-- 3.phpt: Test singular mode with other --FILE-- <?php require_once 'HTML/QuickForm.php'; require_once 'HTML/QuickForm/altselect.php'; $select =& HTML_QuickForm::createElement('altselect', 'test', null, array( 'a' => 'A', 'b' => 'B', 'c' => 'C', )); $select->setIncludeOther(true); echo $select->toHtml(); ?> --EXPECTREGEX-- \<script type\="text\/javascript"\> \/\/\<\!\[CDATA\[ function _qf_altselect_disableElement\(e,disable\) \{ if \(\!disable\) \{ e\.disabled \= false; e\.style\.backgroundColor \= '#ffffff'; \} else \{ e\.disabled \= true; e\.style\.backgroundColor \= '#cccccc'; \} \} \/\/\]\]\> \<\/script\> \<input name\="test" value\="a" type\="radio" id\="qf_([0-9a-z]+)" onclick\="_qf_altselect_disableElement\(this\.form\.elements\[this\.name \+ '_qf_other'\],true\);" \/\>\<label for\="qf_\1"\>A\<\/label\>\<br \/\> \<input name\="test" value\="b" type\="radio" id\="qf_([0-9a-z]+)" onclick\="_qf_altselect_disableElement\(this\.form\.elements\[this\.name \+ '_qf_other'\],true\);" \/\>\<label for\="qf_\2"\>B\<\/label\>\<br \/\> \<input name\="test" value\="c" type\="radio" id\="qf_([0-9a-z]+)" onclick\="_qf_altselect_disableElement\(this\.form\.elements\[this\.name \+ '_qf_other'\],true\);" \/\>\<label for\="qf_\3"\>C\<\/label\>\<br \/\> \<input name\="test" value\="_qf_other" type\="radio" id\="qf_([0-9a-z]+)" onclick\="_qf_altselect_disableElement\(this\.form\.elements\[this\.name\+'_qf_other'\],false\);this\.form\.elements\[this\.name\+'_qf_other'\]\.focus\(\);this\.form\.elements\[this\.name\+'_qf_other'\]\.select\(\);" \/\>\<label for\="qf_\4"\>Other\<\/label\>\<br \/\> \<label for\="qf_([0-9a-z]+)"\>If other please specify\:\<\/label\> \<input id\="qf_\5" name\="test_qf_other" type\="text" \/\> \<script type\="text\/javascript"\> \/\/\<\!\[CDATA\[ _qf_altselect_disableElement\(document\.getElementById\('qf_[0-9a-z]+'\),true\); \/\/\]\]\> \<\/script\> http://cvs.php.net/viewvc.cgi/pear/HTML_QuickForm_altselect/tests/04.phpt?view=markup&rev=1.1 Index: pear/HTML_QuickForm_altselect/tests/04.phpt +++ pear/HTML_QuickForm_altselect/tests/04.phpt --TEST-- 4.phpt: Test multiple mode with other --FILE-- <?php require_once 'HTML/QuickForm.php'; require_once 'HTML/QuickForm/altselect.php'; $select =& HTML_QuickForm::createElement('altselect', 'test', null, array( 'a' => 'A', 'b' => 'B', 'c' => 'C', )); $select->setMultiple(true); $select->setIncludeOther(true); echo $select->toHtml(); ?> --EXPECTREGEX-- \<input name\="test\[\]" type\="checkbox" value\="a" id\="qf_([0-9a-z]+)" \/\>\<label for\="qf_\1"\>A\<\/label\>\<br \/\> \<input name\="test\[\]" type\="checkbox" value\="b" id\="qf_([0-9a-z]+)" \/\>\<label for\="qf_\2"\>B\<\/label\>\<br \/\> \<input name\="test\[\]" type\="checkbox" value\="c" id\="qf_([0-9a-z]+)" \/\>\<label for\="qf_\3"\>C\<\/label\>\<br \/\> \<label for\="qf_([0-9a-z]+)"\>Other\:\<\/label\> \<input id\="qf_\4" name\="test\[\]" type\="text" \/\> http://cvs.php.net/viewvc.cgi/pear/HTML_QuickForm_altselect/tests/05.phpt?view=markup&rev=1.1 Index: pear/HTML_QuickForm_altselect/tests/05.phpt +++ pear/HTML_QuickForm_altselect/tests/05.phpt --TEST-- 5.phpt: Test singular mode with other, setting a value --FILE-- <?php require_once 'HTML/QuickForm.php'; require_once 'HTML/QuickForm/altselect.php'; $select =& HTML_QuickForm::createElement('altselect', 'test', null, array( 'a' => 'A', 'b' => 'B', 'c' => 'C', )); $select->setSelected('b','d'); $select->setIncludeOther(true); echo $select->toHtml(); ?> --EXPECTREGEX-- \<script type\="text\/javascript"\> \/\/\<\!\[CDATA\[ function _qf_altselect_disableElement\(e,disable\) \{ if \(\!disable\) \{ e\.disabled \= false; e\.style\.backgroundColor \= '#ffffff'; \} else \{ e\.disabled \= true; e\.style\.backgroundColor \= '#cccccc'; \} \} \/\/\]\]\> \<\/script\> \<input name\="test" value\="a" type\="radio" id\="qf_([0-9a-z]+)" onclick\="_qf_altselect_disableElement\(this\.form\.elements\[this\.name \+ '_qf_other'\],true\);" \/\>\<label for\="qf_\1"\>A\<\/label\>\<br \/\> \<input name\="test" value\="b" type\="radio" id\="qf_([0-9a-z]+)" onclick\="_qf_altselect_disableElement\(this\.form\.elements\[this\.name \+ '_qf_other'\],true\);" checked\="checked" \/\>\<label for\="qf_\2"\>B\<\/label\>\<br \/\> \<input name\="test" value\="c" type\="radio" id\="qf_([0-9a-z]+)" onclick\="_qf_altselect_disableElement\(this\.form\.elements\[this\.name \+ '_qf_other'\],true\);" \/\>\<label for\="qf_\3"\>C\<\/label\>\<br \/\> \<input name\="test" value\="_qf_other" type\="radio" id\="qf_([0-9a-z]+)" onclick\="_qf_altselect_disableElement\(this\.form\.elements\[this\.name\+'_qf_other'\],false\);this\.form\.elements\[this\.name\+'_qf_other'\]\.focus\(\);this\.form\.elements\[this\.name\+'_qf_other'\]\.select\(\);" \/\>\<label for\="qf_\4"\>Other\<\/label\>\<br \/\> \<label for\="qf_([0-9a-z]+)"\>If other please specify\:\<\/label\> \<input id\="qf_\5" name\="test_qf_other" type\="text" \/\> \<script type\="text\/javascript"\> \/\/\<\!\[CDATA\[ _qf_altselect_disableElement\(document\.getElementById\('qf_\5'\),true\); \/\/\]\]\> \<\/script\> http://cvs.php.net/viewvc.cgi/pear/HTML_QuickForm_altselect/tests/06.phpt?view=markup&rev=1.1 Index: pear/HTML_QuickForm_altselect/tests/06.phpt +++ pear/HTML_QuickForm_altselect/tests/06.phpt --TEST-- 6.phpt: Test multiple mode without other, setting a value --FILE-- <?php require_once 'HTML/QuickForm.php'; require_once 'HTML/QuickForm/altselect.php'; $select =& HTML_QuickForm::createElement('altselect', 'test', null, array( 'a' => 'A', 'b' => 'B', 'c' => 'C', )); $select->setSelected('b','d'); $select->setMultiple(true); $select->setIncludeOther(true); echo $select->toHtml(); ?> --EXPECTREGEX-- \<input name\="test\[\]" type\="checkbox" value\="a" id\="qf_([0-9a-z]+)" \/\>\<label for\="qf_\1"\>A\<\/label\>\<br \/\> \<input name\="test\[\]" type\="checkbox" value\="b" id\="qf_([0-9a-z]+)" checked\="checked" \/\>\<label for\="qf_\2"\>B\<\/label\>\<br \/\> \<input name\="test\[\]" type\="checkbox" value\="c" id\="qf_([0-9a-z]+)" \/\>\<label for\="qf_\3"\>C\<\/label\>\<br \/\> \<label for\="qf_([0-9a-z]+)"\>Other\:\<\/label\> \<input id\="qf_\4" name\="test\[\]" type\="text" \/\> http://cvs.php.net/viewvc.cgi/pear/HTML_QuickForm_altselect/tests/07.phpt?view=markup&rev=1.1 Index: pear/HTML_QuickForm_altselect/tests/07.phpt +++ pear/HTML_QuickForm_altselect/tests/07.phpt --TEST-- 7.phpt: Test singular mode without other acting as a group when accepted by a renderer. --FILE-- <?php require_once 'HTML/QuickForm.php'; require_once 'HTML/QuickForm/Renderer/Array.php'; require_once 'HTML/QuickForm/altselect.php'; $select =& HTML_QuickForm::createElement('altselect', 'test', null, array( 'a' => 'A', 'b' => 'B', 'c' => 'C', )); $select->setGroup(true); $renderer =& new HTML_QuickForm_Renderer_Array; $select->accept($renderer); echo var_export($renderer->toArray(),true); ?> --EXPECTREGEX-- array \( 'elements' \=\> array \( 0 \=\> array \( 'name' \=\> 'test', 'value' \=\> NULL, 'type' \=\> 'group', 'frozen' \=\> false, 'required' \=\> false, 'error' \=\> NULL, 'label' \=\> '', 'separator' \=\> NULL, 'elements' \=\> array \( 0 \=\> array \( 'name' \=\> 'test', 'value' \=\> 'a', 'type' \=\> 'radio', 'frozen' \=\> false, 'required' \=\> false, 'error' \=\> NULL, 'label' \=\> '', 'html' \=\> '\<input name\="test" value\="a" type\="radio" id\="qf_[0-9a-z]+" \/\>', \), 1 \=\> array \( 'name' \=\> 'test', 'value' \=\> 'b', 'type' \=\> 'radio', 'frozen' \=\> false, 'required' \=\> false, 'error' \=\> NULL, 'label' \=\> '', 'html' \=\> '\<input name\="test" value\="b" type\="radio" id\="qf_[0-9a-z]+" \/\>', \), 2 \=\> array \( 'name' \=\> 'test', 'value' \=\> 'c', 'type' \=\> 'radio', 'frozen' \=\> false, 'required' \=\> false, 'error' \=\> NULL, 'label' \=\> '', 'html' \=\> '\<input name\="test" value\="c" type\="radio" id\="qf_[0-9a-z]+" \/\>', \), \), \), \), \) http://cvs.php.net/viewvc.cgi/pear/HTML_QuickForm_altselect/tests/08.phpt?view=markup&rev=1.1 Index: pear/HTML_QuickForm_altselect/tests/08.phpt +++ pear/HTML_QuickForm_altselect/tests/08.phpt --TEST-- 8.phpt: Test singular mode without other, freezing element --FILE-- <?php require_once 'HTML/QuickForm.php'; require_once 'HTML/QuickForm/altselect.php'; $select =& HTML_QuickForm::createElement('altselect', 'test', null, array( 'a' => 'A', 'b' => 'B', 'c' => 'C', )); $select->freeze(); echo $select->toHtml(); ?> --EXPECTREGEX-- \<tt\>\( \)\<\/tt\>\<label for\="qf_[0-9a-z]+"\>A\<\/label\>\<br \/\> \<tt\>\( \)\<\/tt\>\<label for\="qf_[0-9a-z]+"\>B\<\/label\>\<br \/\> \<tt\>\( \)\<\/tt\>\<label for\="qf_[0-9a-z]+"\>C\<\/label\> http://cvs.php.net/viewvc.cgi/pear/HTML_QuickForm_altselect/tests/09.phpt?view=markup&rev=1.1 Index: pear/HTML_QuickForm_altselect/tests/09.phpt +++ pear/HTML_QuickForm_altselect/tests/09.phpt --TEST-- 9.phpt: Test exporting altselect, singular mode, with other, regular value submitted --FILE-- <?php require_once 'HTML/QuickForm.php'; require_once 'HTML/QuickForm/altselect.php'; $select =& HTML_QuickForm::createElement('altselect', 'test', null, array( 'a' => 'A', 'b' => 'B', 'c' => 'C', )); $select->setIncludeOther(true); $submitValues = array('test' => 'b'); var_dump($select->exportValue($submitValues)); ?> --EXPECT-- string(1) "b" http://cvs.php.net/viewvc.cgi/pear/HTML_QuickForm_altselect/tests/10.phpt?view=markup&rev=1.1 Index: pear/HTML_QuickForm_altselect/tests/10.phpt +++ pear/HTML_QuickForm_altselect/tests/10.phpt --TEST-- 10.phpt: Test exporting altselect, singular mode, with other, other value submitted --FILE-- <?php require_once 'HTML/QuickForm.php'; require_once 'HTML/QuickForm/altselect.php'; $select =& HTML_QuickForm::createElement('altselect', 'test', null, array( 'a' => 'A', 'b' => 'B', 'c' => 'C', )); $select->setIncludeOther(true); $submitValues = array('test' => '_qf_other', 'test_qf_other' => 'd'); var_dump($select->exportValue($submitValues)); ?> --EXPECT-- string(1) "d" http://cvs.php.net/viewvc.cgi/pear/HTML_QuickForm_altselect/tests/11.phpt?view=markup&rev=1.1 Index: pear/HTML_QuickForm_altselect/tests/11.phpt +++ pear/HTML_QuickForm_altselect/tests/11.phpt --TEST-- 11.phpt: Test exporting altselect, singular mode, with other, with an array submitted --FILE-- <?php require_once 'HTML/QuickForm.php'; require_once 'HTML/QuickForm/altselect.php'; $select =& HTML_QuickForm::createElement('altselect', 'test', null, array( 'a' => 'A', 'b' => 'B', 'c' => 'C', )); $select->setIncludeOther(true); $submitValues = array('test' => array('b','c')); var_dump($select->exportValue($submitValues)); ?> --EXPECT-- string(1) "b" http://cvs.php.net/viewvc.cgi/pear/HTML_QuickForm_altselect/tests/12.phpt?view=markup&rev=1.1 Index: pear/HTML_QuickForm_altselect/tests/12.phpt +++ pear/HTML_QuickForm_altselect/tests/12.phpt --TEST-- 12.phpt: Test exporting altselect, singular mode, with other, with nothing submitted --FILE-- <?php require_once 'HTML/QuickForm.php'; require_once 'HTML/QuickForm/altselect.php'; $select =& HTML_QuickForm::createElement('altselect', 'test', null, array( 'a' => 'A', 'b' => 'B', 'c' => 'C', )); $select->setSelected('d'); $select->setIncludeOther(true); $submitValues = array(); var_dump($select->exportValue($submitValues)); ?> --EXPECT-- string(1) "d" http://cvs.php.net/viewvc.cgi/pear/HTML_QuickForm_altselect/tests/13.phpt?view=markup&rev=1.1 Index: pear/HTML_QuickForm_altselect/tests/13.phpt +++ pear/HTML_QuickForm_altselect/tests/13.phpt --TEST-- 13.phpt: Test exporting altselect, multiple mode, with other --FILE-- <?php require_once 'HTML/QuickForm.php'; require_once 'HTML/QuickForm/altselect.php'; $select =& HTML_QuickForm::createElement('altselect', 'test', null, array( 'a' => 'A', 'b' => 'B', 'c' => 'C', )); $select->setMultiple(true); $select->setIncludeOther(true); $submitValues = array('test' => array('b','c','d')); var_dump($select->exportValue($submitValues)); ?> --EXPECT-- array(3) { [0]=> string(1) "b" [1]=> string(1) "c" [2]=> string(1) "d" } http://cvs.php.net/viewvc.cgi/pear/HTML_QuickForm_altselect/tests/14.phpt?view=markup&rev=1.1 Index: pear/HTML_QuickForm_altselect/tests/14.phpt +++ pear/HTML_QuickForm_altselect/tests/14.phpt --TEST-- 14.phpt: Test exporting altselect, multiple mode, with other, single value submitted --FILE-- <?php require_once 'HTML/QuickForm.php'; require_once 'HTML/QuickForm/altselect.php'; $select =& HTML_QuickForm::createElement('altselect', 'test', null, array( 'a' => 'A', 'b' => 'B', 'c' => 'C', )); $select->setMultiple(true); $select->setIncludeOther(true); $submitValues = array('test' => 'c'); var_dump($select->exportValue($submitValues)); ?> --EXPECT-- array(1) { [0]=> string(1) "c" } http://cvs.php.net/viewvc.cgi/pear/HTML_QuickForm_altselect/tests/15.phpt?view=markup&rev=1.1 Index: pear/HTML_QuickForm_altselect/tests/15.phpt +++ pear/HTML_QuickForm_altselect/tests/15.phpt --TEST-- 15.phpt: Test exporting altselect, multiple mode, with other, nothing submitted --FILE-- <?php require_once 'HTML/QuickForm.php'; require_once 'HTML/QuickForm/altselect.php'; $select =& HTML_QuickForm::createElement('altselect', 'test', null, array( 'a' => 'A', 'b' => 'B', 'c' => 'C', )); $select->setMultiple(true); $select->setSelected(array('b','c')); $select->setIncludeOther(true); $submitValues = array(); var_dump($select->exportValue($submitValues)); ?> --EXPECT-- array(2) { [0]=> string(1) "b" [1]=> string(1) "c" }
« previous php.pear.cvs (#44649) next »