Custom 2

Overview

This example will display a dual multi-select QuickForm element, to manage a simple fruit list.
Both lists have headers, buttons using images (png) are between lists (vertical position), and colors are managed by CSS rules.

Screenshot

sample screenshot

Demonstration

Give it a try

Dependencies

This example requires mandatory resources :

Explains step by step

Step 1

The dual multi-select is created (lines 58 to 62) with headers and load with data defined from $fruit_array (lines 43 thru 53).

Both list are 300 pixels width and display 5 items (lines 59-60). Styles are defined by CSS rules and class pool (lines 100-110).

Property Value Default
size 5 10
width 300 100
class pool  

Step 2

The basic render is replaced by a new layout definition: vertical list position (lines 68-81) set at line 82.

Step 3

The swap buttons uses now png images (lines 64-65).

Property Value Default
name   add
value    >> 
type image button
src /img/qfams/down.png  

Property Value Default
name   remove
value    << 
type image button
src /img/qfams/up.png  

Source Code

PHP code
  1. <?php
  2. /**
  3.  * Custom advMultiSelect HTML_QuickForm element
  4.  * using images for 'add' and 'remove' buttons.
  5.  *
  6.  * The template allows to add label as headers of dual select box
  7.  * and moves the buttons between the two select box (up and down).
  8.  *
  9.  * @version    $Id: qfams_custom_2.php,v 1.5 2009/01/28 22:24:43 farell Exp $
  10.  * @author     Laurent Laville <pear@laurent-laville.org>
  11.  * @package    HTML_QuickForm_advmultiselect
  12.  * @subpackage Examples
  13.  * @access     public
  14.  * @example    examples/qfams_custom_2.php
  15.  *             qfams_custom_2 source code
  16.  * @link       http://www.laurent-laville.org/img/qfams/screenshot/custom2.png
  17.  *             screenshot (Image PNG, 374x302 pixels) 5.80 Kb
  18.  */
  19.  
  20. require_once 'HTML/QuickForm.php';
  21. require_once 'HTML/QuickForm/advmultiselect.php';
  22.  
  23. $form = new HTML_QuickForm('amsCustom2');
  24. $form->removeAttribute('name');        // XHTML compliance
  25.  
  26. // same as default element template but wihtout the label (in first td cell)
  27. $withoutLabel = <<<_HTML
  28. <tr valign="top">
  29.     <td align="right">
  30.         &nbsp;
  31.     </td>
  32.     <td align="left">
  33.         <!-- BEGIN error --><span style="color: #ff0000;">{error}</span><br /><!-- END error -->{element}
  34.     </td>
  35. </tr>
  36. _HTML;
  37.  
  38. // more XHTML compliant
  39. // replace default element template with label, because submit button have no label
  40. $renderer =& $form->defaultRenderer();
  41. $renderer->setElementTemplate($withoutLabel, 'send');
  42.  
  43. $fruit_array = array(
  44.     'apple'     =>  'Apple',
  45.     'orange'    =>  'Orange',
  46.     'pear'      =>  'Pear',
  47.     'banana'    =>  'Banana',
  48.     'cherry'    =>  'Cherry',
  49.     'kiwi'      =>  'Kiwi',
  50.     'lemon'     =>  'Lemon',
  51.     'lime'      =>  'Lime',
  52.     'tangerine' =>  'Tangerine',
  53. );
  54.  
  55. // rendering with QF renderer engine and template system
  56. $form->addElement('header', null, 'Advanced Multiple Select: custom layout ');
  57.  
  58. $ams =& $form->addElement('advmultiselect', 'fruit', null, $fruit_array,
  59.                            array('size' => 5,
  60.                                  'class' => 'pool', 'style' => 'width:300px;'
  61.                                 )
  62. );
  63. $ams->setLabel(array('Fruit:', 'Available', 'Selected'));
  64. $ams->setButtonAttributes('add',    array('type' => 'image', 'src' => '/img/qfams/down.png'));
  65. $ams->setButtonAttributes('remove', array('type' => 'image', 'src' => '/img/qfams/up.png'));
  66.  
  67. // vertical select box with image buttons as selector
  68. $template = '
  69. <table{class}>
  70. <!-- BEGIN label_2 --><tr><th align="center">{label_2}</th></tr><!-- END label_2 -->
  71. <tr>
  72.  <td>{unselected}</td>
  73. </tr>
  74. <tr>
  75.  <td align="center">{add}{remove}</td>
  76. </tr>
  77. <tr>
  78.  <td>{selected}</td>
  79. </tr>
  80. <!-- BEGIN label_3 --><tr><th align="center">{label_3}</th></tr><!-- END label_3 -->
  81. </table>';
  82. $ams->setElementTemplate($template);
  83.  
  84. $form->addElement('submit', 'send', 'Send');
  85. ?>
  86. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  87.     "http://www.w3c.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  88. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
  89. <head>
  90. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  91. <title>HTML_QuickForm::advMultiSelect custom example 2</title>
  92. <style type="text/css">
  93. <!--
  94. body {
  95.   background-color: #FFF;
  96.   font-family: Verdana, Arial, helvetica;
  97.   font-size: 10pt;
  98. }
  99.  
  100. table.pool {
  101.   border: 0;
  102. }
  103. table.pool th {
  104.   font-size: 80%;
  105.   font-style: italic;
  106.   text-align: center;
  107. }
  108. table.pool select {
  109.   background-color: lightyellow;
  110. }
  111.  -->
  112. </style>
  113. <?php echo $ams->getElementJs(false); ?>
  114. </head>
  115. <body>
  116. <?php
  117. if ($form->validate()) {
  118.     $clean = $form->getSubmitValues();
  119.  
  120.     echo '<pre>';
  121.     print_r($clean);
  122.     echo '</pre>';
  123. }
  124. $form->display();
  125. ?>
  126. </body>
  127. </html>
generated by Generic Syntax Highlighter - GeSHi