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

Source for file array-twig.php

Documentation is available at array-twig.php

  1. <?php
  2. /**
  3.  * Usage example for HTML_QuickForm2 package: Array renderer with Twig template engine
  4.  *
  5.  * For this example to run, you need to install Twig template engine
  6.  * from its separate PEAR channel:
  7.  * <code>
  8.  * $ pear channel-discover pear.twig-project.org
  9.  * $ pear install twig/Twig
  10.  * </code>
  11.  */
  12.  
  13. require_once 'HTML/QuickForm2.php';
  14. require_once 'HTML/QuickForm2/Renderer.php';
  15.  
  16. if (!class_exists('Twig_Environment', true)) {
  17.     require_once 'Twig/Autoloader.php';
  18.     Twig_Autoloader::register();
  19. }
  20.  
  21. $options = array(
  22.     'a' => 'Letter A', 'b' => 'Letter B', 'c' => 'Letter C',
  23.     'd' => 'Letter D', 'e' => 'Letter E', 'f' => 'Letter F'
  24. );
  25.  
  26. $main = array("Pop", "Rock", "Classical");
  27.  
  28. $secondary = array(
  29.     array(0 => "Belle & Sebastian", 1 => "Elliot Smith", 2 => "Beck"),
  30.     array(3 => "Noir Desir", 4 => "Violent Femmes"),
  31.     array(5 => "Wagner", 6 => "Mozart", 7 => "Beethoven")
  32. );
  33.  
  34. $form = new HTML_QuickForm2('elements');
  35.  
  36. // data source with default values:
  37. $form->addDataSource(new HTML_QuickForm2_DataSource_Array(array(
  38.     'textTest'        => 'Some text',
  39.     'areaTest'        => "Some text\non multiple lines",
  40.     'userTest'        => 'luser',
  41.     'selSingleTest'   => 'f',
  42.     'selMultipleTest' => array('b', 'c'),
  43.     'boxTest'         => '1',
  44.     'radioTest'       => '2',
  45.     'testDate'        => time(),
  46.     'testHierselect'  => array(2, 5)
  47. )));
  48.  
  49. // text input elements
  50. $fsText = $form->addElement('fieldset')->setLabel('Text boxes');
  51. $fsText->addElement(
  52.     'text', 'textTest', array('style' => 'width: 300px;'), array('label' => 'Test Text:')
  53. )->addRule('required', 'This field is required');
  54. $fsText->addElement(
  55.     'password', 'pwdTest', array('style' => 'width: 300px;'), array('label' => 'Test Password:')
  56. );
  57. $area = $fsText->addElement(
  58.     'textarea', 'areaTest', array('style' => 'width: 300px;', 'cols' => 50, 'rows' => 7),
  59.     array('label' => 'Test Textarea:')
  60. );
  61. $area->addRule('required', 'This field is required');
  62.  
  63. $fsNested = $form->addElement('fieldset')->setLabel('Nested fieldset');
  64. $fsNested->addElement(
  65.     'text', 'userTest', array('style' => 'width: 200px'), array('label' => 'Username:')
  66. );
  67. $fsNested->addElement(
  68.     'password', 'passTest', array('style' => 'width: 200px'), array('label' => 'Password:')
  69. );
  70. // Now we move the fieldset into another fieldset!
  71. $fsText->insertBefore($fsNested, $area);
  72.  
  73.  
  74. // selects
  75. $fsSelect = $form->addElement('fieldset')->setLabel('Selects');
  76. $fsSelect->addElement(
  77.     'select', 'selSingleTest', null, array('options' => $options, 'label' => 'Single select:')
  78. );
  79. $fsSelect->addElement(
  80.     'select', 'selMultipleTest', array('multiple' => 'multiple', 'size' => 4),
  81.     array('options' => $options, 'label' => 'Multiple select:')
  82. );
  83.  
  84. // checkboxes and radios
  85. $fsCheck = $form->addElement('fieldset')->setLabel('Checkboxes and radios');
  86. $fsCheck->addElement(
  87.     'checkbox', 'boxTest', null, array('content' => 'check me', 'label' => 'Test Checkbox:')
  88. );
  89. $fsCheck->addElement(
  90.     'radio', 'radioTest', array('value' => 1), array('content' => 'select radio #1', 'label' => 'Test radio:')
  91. );
  92. $fsCheck->addElement(
  93.     'radio', 'radioTest', array('value' => 2), array('content' => 'select radio #2', 'label' => '(continued)')
  94. );
  95.  
  96. $fsCustom = $form->addElement('fieldset')->setLabel('Custom elements');
  97. $fsCustom->addElement(
  98.     'date', 'testDate', null,
  99.     array('format' => 'd-F-Y', 'minYear' => date('Y'), 'maxYear' => 2001)
  100. )->setLabel('Today is:');
  101.  
  102. $fsCustom->addElement('hierselect', 'testHierselect', array('style' => 'width: 20em;'))
  103.          ->setLabel('Hierarchical select:')
  104.          ->loadOptions(array($main, $secondary))
  105.          ->setSeparator('<br />');
  106.  
  107. // buttons
  108. $fsButton = $form->addElement('fieldset')->setLabel('Buttons');
  109. $testReset = $fsButton->addElement(
  110.     'reset', 'testReset', array('value' => 'This is a reset button')
  111. );
  112. $fsButton->addElement(
  113.     'inputbutton', 'testInputButton',
  114.     array('value' => 'Click this button', 'onclick' => "alert('This is a test.');")
  115. );
  116. $fsButton->addElement(
  117.     'button', 'testButton', array('onclick' => "alert('Almost nothing');", 'type' => 'button'),
  118.     array('content' => '<img src="http://pear.php.net/gifs/pear-icon.gif" '.
  119.           'width="32" height="32" alt="pear" />This button does almost nothing')
  120. );
  121. // submit buttons in nested fieldset
  122. $fsSubmit = $fsButton->addElement('fieldset')->setLabel('These buttons can submit the form');
  123. $fsSubmit->addElement(
  124.     'submit', 'testSubmit', array('value' => 'Test Submit')
  125. );
  126. $fsSubmit->addElement(
  127.     'button', 'testSubmitButton', array('type' => 'submit'),
  128.     array('content' => '<img src="http://pear.php.net/gifs/pear-icon.gif" '.
  129.           'width="32" height="32" alt="pear" />This button submits')
  130. );
  131. $fsSubmit->addElement(
  132.     'image', 'testImage', array('src' => 'http://pear.php.net/gifs/pear-icon.gif')
  133. );
  134.  
  135. $context = array();
  136. // outputting form values
  137. if ($form->validate()) {
  138.     $context['submitvalues'] = print_r($form->getValue(), true);
  139.     // let's freeze the form and remove the reset button
  140.     $fsButton->removeChild($testReset);
  141.     $form->toggleFrozen(true);
  142. }
  143.  
  144. if ('@data_dir@' != '@' . 'data_dir@') {
  145.     $filename = '@data_dir@/HTML_QuickForm2/quickform.css';
  146. } else {
  147.     $filename = dirname(dirname(dirname(dirname(__FILE__)))) . '/data/quickform.css';
  148. }
  149. $context['default_styles'] = file_get_contents($filename);
  150.  
  151. $renderer = HTML_QuickForm2_Renderer::factory('array');
  152. $form->render($renderer);
  153.  
  154. $loader   = new Twig_Loader_Filesystem(dirname(__FILE__) . '/templates');
  155. // in real life usage you should set up the cache directory!
  156. $twig     = new Twig_Environment($loader);
  157. $template = $twig->loadTemplate('array-twig.tpl');
  158.  
  159. $template->display($context + array(
  160.     'js_libraries' => $renderer->getJavascriptBuilder()->getLibraries(true, true),
  161.     'form'         => $renderer->toArray()
  162. ));
  163. ?>

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