Basic 1

Overview

This example will display a dual multi-select QuickForm element, to manage a simple car list.
Only default options are used.

Screenshot

sample screenshot

Demonstration

Give it a try

Dependencies

This example requires mandatory resources :

Explains step by step

Step 1

First, we loads the class definitions for QuickForm and advmultiselect element (lines 17,18), then we creates a HTML_QuickForm object that will contain the object representing elements and all the other necessary information. We only pass the form's name to the constructor, which means that default values will be used for other parameters (line 20).

The dual multi-select is created (line 55) with no headers except global element title 'Cars:' and load with data defined from $car_array (lines 40 thru 50).

Both list are 100 pixels width and display 10 items:

Property Value Default
size   10
width   100
class    

Step 2

The basic render is built with default element template and default class declarations: none means (table border=0 cellpadding=10 cellspacing=0).

Here is the default element template:

{javascript}
<table{class}>
<!-- BEGIN label_2 --><tr><th>{label_2}</th><!-- END label_2 -->
<!-- BEGIN label_3 --><th>&nbsp;</th><th>{label_3}</th></tr><!-- END label_3 -->
<tr>
  <td valign="top">{unselected}</td>
  <td align="center">{add}{remove}</td>
  <td valign="top">{selected}</td>
</tr>
</table>

The swap buttons used defaults that may be retrieve if you don't specify attributes on setButtonAttributes() method.

Property Value Default
name   add
value    >> 
type   button

Property Value Default
name   remove
value    << 
type   button

Step 3

Immediately before the form is displayed (line 84) we attempt to validate it (line 77). Results of car selection are displayed on lines lines 78 to 82.

Source Code

PHP code
  1. <?php
  2. /**
  3.  * Basic advMultiSelect HTML_QuickForm element
  4.  * without any customization.
  5.  *
  6.  * @version    $Id: qfams_basic_1.php,v 1.5 2009/01/28 22:24:43 farell Exp $
  7.  * @author     Laurent Laville <pear@laurent-laville.org>
  8.  * @package    HTML_QuickForm_advmultiselect
  9.  * @subpackage Examples
  10.  * @access     public
  11.  * @example    examples/qfams_basic_1.php
  12.  *             qfams_basic_1 source code
  13.  * @link       http://www.laurent-laville.org/img/qfams/screenshot/basic1.png
  14.  *             screenshot (Image PNG, 406x247 pixels) 4.95 Kb
  15.  */
  16.  
  17. require_once 'HTML/QuickForm.php';
  18. require_once 'HTML/QuickForm/advmultiselect.php';
  19.  
  20. $form = new HTML_QuickForm('amsBasic1');
  21. $form->removeAttribute('name');        // XHTML compliance
  22.  
  23. // same as default element template but wihtout the label (in first td cell)
  24. $withoutLabel = <<<_HTML
  25. <tr valign="top">
  26.     <td align="right">
  27.         &nbsp;
  28.     </td>
  29.     <td align="left">
  30.         <!-- BEGIN error --><span style="color: #ff0000;">{error}</span><br /><!-- END error -->{element}
  31.     </td>
  32. </tr>
  33. _HTML;
  34.  
  35. // more XHTML compliant
  36. // replace default element template with label, because submit button have no label
  37. $renderer =& $form->defaultRenderer();
  38. $renderer->setElementTemplate($withoutLabel, 'send');
  39.  
  40. $car_array = array(
  41.     'dodge'     =>  'Dodge',
  42.     'chevy'     =>  'Chevy',
  43.     'bmw'       =>  'BMW',
  44.     'audi'      =>  'Audi',
  45.     'porsche'   =>  'Porsche',
  46.     'kia'       =>  'Kia',
  47.     'subaru'    =>  'Subaru',
  48.     'mazda'     =>  'Mazda',
  49.     'isuzu'     =>  'Isuzu',
  50. );
  51.  
  52. // rendering with all default options
  53. $form->addElement('header', null, 'Advanced Multiple Select: default layout ');
  54.  
  55. $form->addElement('advmultiselect', 'cars', 'Cars:', $car_array);
  56.  
  57. $form->addElement('submit', 'send', 'Send');
  58. ?>
  59. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  60.     "http://www.w3c.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  61. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
  62. <head>
  63. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  64. <title>HTML_QuickForm::advMultiSelect basic example 1</title>
  65. <style type="text/css">
  66. <!--
  67. body {
  68.   background-color: #FFF;
  69.   font-family: Verdana, Arial, helvetica;
  70.   font-size: 10pt;
  71. }
  72.  -->
  73. </style>
  74. </head>
  75. <body>
  76. <?php
  77. if ($form->validate()) {
  78.     $clean = $form->getSubmitValues();
  79.  
  80.     echo '<pre>';
  81.     print_r($clean);
  82.     echo '</pre>';
  83. }
  84. $form->display();
  85. ?>
  86. </body>
  87. </html>
generated by Generic Syntax Highlighter - GeSHi