HTML_QuickForm2
[ class tree: HTML_QuickForm2 ] [ index: HTML_QuickForm2 ] [ all elements ]

Source for file tabbed.php

Documentation is available at tabbed.php

  1. <?php
  2. /**
  3.  * Usage example for HTML_QuickForm2_Controller: tabbed form
  4.  *
  5.  * @author  Alexey Borzov <avb@php.net>
  6.  * @author  Bertrand Mansion <php@mamasam.com>
  7.  * @ignore
  8.  */
  9.  
  10. require_once 'HTML/QuickForm2.php';
  11. require_once 'HTML/QuickForm2/Controller.php';
  12. require_once 'HTML/QuickForm2/Renderer.php';
  13.  
  14. // Load some default action handlers
  15. require_once 'HTML/QuickForm2/Controller/Action/Submit.php';
  16. require_once 'HTML/QuickForm2/Controller/Action/Jump.php';
  17. require_once 'HTML/QuickForm2/Controller/Action/Direct.php';
  18. require_once 'HTML/QuickForm2/Controller/Action/Display.php';
  19.  
  20. // Start the session, form-page values will be kept there
  21.  
  22. abstract class TabbedPage extends HTML_QuickForm2_Controller_Page
  23. {
  24.     protected function addTabs()
  25.     {
  26.         $tabGroup $this->form->addElement('group')->setSeparator('&nbsp;')
  27.                                ->setId('tabs');
  28.         foreach ($this->getController(as $pageId => $page{
  29.             $tabGroup->addElement('submit'$this->getButtonName($pageId),
  30.                                   array('class' => 'flat''value' => ucfirst($pageId)) +
  31.                                   ($page === $this? array('disabled' => 'disabled'): array()));
  32.         }
  33.     }
  34.  
  35.     protected function addGlobalSubmit()
  36.     {
  37.         $this->form->addElement('submit'$this->getButtonName('submit'),
  38.                                 array('value' => 'Big Red Button''class' => 'bigred'));
  39.         $this->setDefaultAction('submit');
  40.     }
  41. }
  42.  
  43. class PageFoo extends TabbedPage
  44. {
  45.     protected function populateForm()
  46.     {
  47.         $this->addTabs();
  48.  
  49.         $fs $this->form->addElement('fieldset')->setLabel('Foo page');
  50.  
  51.         $radioGroup $fs->addElement('group')->setLabel('Do you want this feature?')
  52.                          ->setSeparator('<br />');
  53.         $radioGroup->addElement('radio''iradYesNoMaybe'array('value' => 'Y')array('content' => 'Yes'));
  54.         $radioGroup->addElement('radio''iradYesNoMaybe'array('value' => 'N')array('content' => 'No'));
  55.         $radioGroup->addElement('radio''iradYesNoMaybe'array('value' => 'M')array('content' => 'Maybe'));
  56.  
  57.         $fs->addElement('text''tstText'array('size'=>20'maxlength'=>50))
  58.            ->setLabel('Why do you want it?');
  59.  
  60.         $radioGroup->addRule('required''Check a radiobutton');
  61.  
  62.         $this->addGlobalSubmit();
  63.     }
  64. }
  65.  
  66. class PageBar extends TabbedPage
  67. {
  68.     protected function populateForm()
  69.     {
  70.         $this->addTabs();
  71.  
  72.         $fs $this->form->addElement('fieldset')->setLabel('Bar page');
  73.  
  74.         // XXX: no date element yet
  75.         $dateGroup $fs->addElement('group''favDate')->setLabel('Favourite date:')
  76.                         ->setSeparator('-');
  77.         for ($i = 1$doptions = array()$i <= 31; $i++{
  78.             $doptions[$isprintf('%02d'$i);
  79.         }
  80.         $dateGroup->addElement('select''d')->loadOptions($doptions);
  81.         $dateGroup->addElement('select''M')->loadOptions(array(
  82.             1 => 'January'2 => 'February'3 => 'March'4 => 'April'5 => 'May'5 => 'June',
  83.             7 => 'July'8 => 'August'9 => 'September'10 => 'October'11 => 'November'12 => 'December'
  84.         ));
  85.         for ($i = 1950$yoptions = array()$i <= date('Y')$i++{
  86.             $yoptions[$i$i;
  87.         }
  88.         $dateGroup->addElement('select''Y')->loadOptions($yoptions);
  89.  
  90.  
  91.         $checkGroup $fs->addElement('group''favLetter')->setLabel('Favourite letters:')
  92.                          ->setSeparator(array('&nbsp;''<br />'));
  93.         foreach (array('A''B''C''D''X''Y''Z'as $letter{
  94.             $checkGroup->addElement('checkbox'$letter)->setContent($letter);
  95.         }
  96.  
  97.         $this->addGlobalSubmit();
  98.     }
  99. }
  100.  
  101. class PageBaz extends TabbedPage
  102. {
  103.     protected function populateForm()
  104.     {
  105.         $this->addTabs();
  106.  
  107.         $fs $this->form->addElement('fieldset')->setLabel('Baz page');
  108.  
  109.         $poem $fs->addElement('textarea''textPoetry'array('rows' => 5'cols' => 40))
  110.                    ->setLabel('Recite a poem:');
  111.         $fs->addElement('textarea''textOpinion'array('rows' => 5'cols' => 40))
  112.            ->setLabel('Did you like this demo?');
  113.  
  114.         $poem->addRule('required''Pretty please!');
  115.  
  116.         $this->addGlobalSubmit();
  117.     }
  118. }
  119.  
  120. {
  121.     protected function renderForm(HTML_QuickForm2 $form)
  122.     {
  123. ?>
  124. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  125.     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  126. <html xmlns="http://www.w3.org/1999/xhtml">
  127.   <head>
  128.     <style type="text/css">
  129. /* Set up custom font and form width */
  130. body {
  131.     margin-left: 10px;
  132.     font-family: Arial,sans-serif;
  133.     font-size: small;
  134. }
  135.  
  136. .quickform {
  137.     min-width: 500px;
  138.     max-width: 600px;
  139.     width: 560px;
  140. }
  141.  
  142. .quickform input.bigred {font-weight: bold; background: #FF6666;}
  143. .quickform input.flat {border-style: solid; border-width: 2px; border-color: #000000;}
  144.  
  145. /* Use default styles included with the package */
  146.  
  147. <?php
  148.     if ('@data_dir@' != '@' 'data_dir@'{
  149.         $filename '@data_dir@/HTML_QuickForm2/quickform.css';
  150.     else {
  151.         $filename dirname(dirname(dirname(__FILE__))) '/data/quickform.css';
  152.     }
  153.     readfile($filename);
  154. ?>
  155.     </style>
  156.     <title>HTML_QuickForm2 simple controller example</title>
  157.   </head>
  158.   <body>
  159. <?php
  160.     $renderer HTML_QuickForm2_Renderer::factory('default');
  161.     $renderer->setTemplateForId('tabs''<div style="float: right;">{content}</div>');
  162.     echo $form->render($renderer);
  163. ?>
  164.   </body>
  165. </html>
  166. <?php
  167.     }
  168. }
  169.  
  170. {
  171.     public function perform(HTML_QuickForm2_Controller_Page $page$name)
  172.     {
  173.         echo "Submit successful!<br>\n<pre>\n";
  174.         var_dump($page->getController()->getValue());
  175.         echo "\n</pre>\n";
  176.     }
  177. }
  178.  
  179.  
  180. $tabbed = new HTML_QuickForm2_Controller('Tabbed'false);
  181.  
  182. $tabbed->addPage(new PageFoo(new HTML_QuickForm2('foo')));
  183. $tabbed->addPage(new PageBar(new HTML_QuickForm2('bar')));
  184. $tabbed->addPage(new PageBaz(new HTML_QuickForm2('baz')));
  185.  
  186. // These actions manage going directly to the pages with the same name
  187. $tabbed->addHandler('foo'new HTML_QuickForm2_Controller_Action_Direct());
  188. $tabbed->addHandler('bar'new HTML_QuickForm2_Controller_Action_Direct());
  189. $tabbed->addHandler('baz'new HTML_QuickForm2_Controller_Action_Direct());
  190.  
  191. // We actually add these handlers here for the sake of example
  192. // They can be automatically loaded and added by the controller
  193. $tabbed->addHandler('submit'new HTML_QuickForm2_Controller_Action_Submit());
  194. $tabbed->addHandler('jump'new HTML_QuickForm2_Controller_Action_Jump());
  195.  
  196. // This is the action we should always define ourselves
  197. $tabbed->addHandler('process'new TabbedProcess());
  198. // We redefine 'display' handler to use the proper stylesheets
  199. $tabbed->addHandler('display'new TabbedDisplay());
  200.  
  201. $tabbed->addDatasource(new HTML_QuickForm2_DataSource_Array(array(
  202.     'iradYesNoMaybe' => 'M',
  203.     'favLetter'      => array('A' => true'Z' => true),
  204.     'favDate'        => array('d' => 1'M' => 1'Y' => 2001),
  205.     'textOpinion'    => 'Yes, it rocks!'
  206. )));
  207.  
  208. $tabbed->run();
  209. ?>

Documentation generated on Wed, 10 Apr 2019 08:56:11 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.