home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 June / ENTER.ISO / files / xampp-win32-1.4.5-installer.exe / xampp / elements.php < prev    next >
Encoding:
PHP Script  |  2004-03-24  |  6.4 KB  |  155 lines

  1. <?php
  2. /**
  3. * Example of usage for PEAR class HTML_QuickForm
  4. *
  5. * @author      Adam Daniel <adaniel1@eesus.jnj.com>
  6. * @author      Bertrand Mansion <bmansion@mamasam.com>
  7. * @author      Alexey Borzov <avb@php.net>
  8. * @version     3.2
  9. *
  10. * $Id: elements.php,v 1.2 2004/03/22 10:05:09 mansion Exp $ 
  11. */
  12.  
  13. require_once 'HTML/QuickForm.php';
  14.  
  15. $form =& new HTML_QuickForm('frmTest', 'get');
  16.  
  17. // Use a two-label template for the elements that require some comments
  18. $twoLabel = <<<_HTML
  19. <tr valign="top">
  20.     <td align="right">
  21.         <!-- BEGIN required --><span style="color: #F00;">*</span><!-- END required --><b>{label}</b>
  22.     </td>
  23.     <td align="left">
  24.         <!-- BEGIN error --><span style="color: #F00;">{error}</span><br /><!-- END error -->{element}
  25.         <!-- BEGIN label_2 --><br /><span style="font-size: 80%;">{label_2}</span><!-- END label_2 -->
  26.     </td>
  27. </tr>
  28. _HTML;
  29.  
  30. $renderer =& $form->defaultRenderer();
  31. $renderer->setElementTemplate($twoLabel, 'iadvChk');
  32. $renderer->setElementTemplate($twoLabel, 'iautoComp');
  33.  
  34. // Fills with some defaults values
  35. $form->setDefaults(array(
  36.     'itxtTest'  => 'Test Text Box',
  37.     'itxaTest'  => 'Hello World',
  38.     'ichkTest'  => true,
  39.     'iradTest'  => 1,
  40.     'iselTest'  => array('B', 'C'),
  41.     'name'      => array('first'=>'Adam', 'last'=>'Daniel'),
  42.     'phoneNo'   => array('513', '123', '3456'),
  43.     'iradYesNo' => 'Y',
  44.     'ichkABC'   => array('A'=>true,'B'=>true),
  45.     'dateTest1' => array('d'=>11, 'm'=>1, 'Y'=>2003)
  46. ));
  47.  
  48. $form->setConstants(array(
  49.     'dateTest3' => time()
  50. ));
  51.  
  52. // Elements will be displayed in the order they are declared
  53. $form->addElement('header', '', 'Normal Elements');
  54. // Classic form elements
  55. $form->addElement('hidden', 'ihidTest', 'hiddenField');
  56. $form->addElement('text', 'itxtTest', 'Test Text:');
  57. $form->addElement('textarea', 'itxaTest', 'Test TextArea:', array('rows' => 3, 'cols' => 20));
  58. $form->addElement('password', 'ipwdTest', 'Test Password:');
  59. $form->addElement('checkbox', 'ichkTest', 'Test CheckBox:', 'Check the box');
  60. $form->addElement('radio', 'iradTest', 'Test Radio Buttons:', 'Check the radio button #1', 1);
  61. $form->addElement('radio', 'iradTest', '(Not a group)', 'Check the radio button #2', 2);
  62. $form->addElement('button', 'ibtnTest', 'Test Button', array('onclick' => "alert('This is a test');"));
  63. $form->addElement('reset', 'iresTest', 'Test Reset');
  64. $form->addElement('submit', 'isubTest', 'Test Submit');
  65. $form->addElement('image', 'iimgTest', 'http://pear.php.net/gifs/pear-icon.gif');
  66. $select =& $form->addElement('select', 'iselTest', 'Test Select:', array('A'=>'A', 'B'=>'B','C'=>'C','D'=>'D'));
  67. $select->setSize(5);
  68. $select->setMultiple(true);
  69.  
  70. $form->addElement('header', '', 'Custom Elements');
  71. // Date elements
  72. $form->addElement('date', 'dateTest1', 'Date1:', array('format'=>'dmY', 'minYear'=>2010, 'maxYear'=>2001));
  73. $form->addElement('date', 'dateTest2', 'Date2:', array('format'=>'d-F-Y H:i', 'language'=>'de', 'optionIncrement' => array('i' => 5)));
  74. $form->addElement('date', 'dateTest3', 'Today is:', array('format'=>'l d M Y'));
  75.  
  76. $main[0] = "Pop";
  77. $main[1] = "Rock";
  78. $main[2] = "Classical";
  79.  
  80. $secondary[0][0] = "Belle & Sebastian";
  81. $secondary[0][1] = "Elliot Smith";
  82. $secondary[0][2] = "Beck";
  83. $secondary[1][3] = "Noir Desir";
  84. $secondary[1][4] = "Violent Femmes";
  85. $secondary[2][5] = "Wagner";
  86. $secondary[2][6] = "Mozart";
  87. $secondary[2][7] = "Beethoven";
  88.  
  89. $opts[] = $main;
  90. $opts[] = $secondary;
  91.  
  92. $hs =& $form->addElement('hierselect', 'ihsTest', 'Hierarchical select:', array('style' => 'width: 20em;'), '<br />');
  93. $hs->setOptions($opts);
  94.  
  95. $form->addElement('advcheckbox', 'iadvChk', array('Advanced checkbox:', 'Unlike standard checkbox, this element <b>has</b> a value<br />when it is not checked.'), 'Check the box', null, array('off', 'on'));
  96.  
  97. $form->addElement('autocomplete', 'iautoComp', array('Your favourite fruit:', 'This is autocomplete element.<br />Start typing and see how it suggests possible completions.'), array('Pear', 'Orange', 'Apple'), array('size' => 30));
  98.  
  99.  
  100. $form->addElement('header', '', 'Grouped Elements');
  101. // Grouped elements
  102. $name['last'] = &HTML_QuickForm::createElement('text', 'last', null, array('size' => 30));
  103. $name['first'] = &HTML_QuickForm::createElement('text', 'first', null, array('size' => 20));
  104. $form->addGroup($name, 'name', 'Name (last, first):', ', ');
  105. // Creates a group of text inputs
  106. $areaCode = &HTML_QuickForm::createElement('text', '', null, array('size' => 3, 'maxlength' => 3));
  107. $phoneNo1 = &HTML_QuickForm::createElement('text', '', null, array('size' => 3, 'maxlength' => 3));
  108. $phoneNo2 = &HTML_QuickForm::createElement('text', '', null, array('size' => 4, 'maxlength' => 4));
  109. $form->addGroup(array($areaCode, $phoneNo1, $phoneNo2), 'phoneNo', 'Telephone:', '-');
  110.  
  111. // Creates a radio buttons group
  112. $radio[] = &HTML_QuickForm::createElement('radio', null, null, 'Yes', 'Y');
  113. $radio[] = &HTML_QuickForm::createElement('radio', null, null, 'No', 'N');
  114. $form->addGroup($radio, 'iradYesNo', 'Yes/No:');
  115.  
  116. // Creates a checkboxes group
  117. $checkbox[] = &HTML_QuickForm::createElement('checkbox', 'A', null, 'A');
  118. $checkbox[] = &HTML_QuickForm::createElement('checkbox', 'B', null, 'B');
  119. $checkbox[] = &HTML_QuickForm::createElement('checkbox', 'C', null, 'C');
  120. $form->addGroup($checkbox, 'ichkABC', 'ABC:', '<br />');
  121. // Creates a group of buttons to be displayed at the bottom of the form
  122. $buttons[] = &HTML_QuickForm::createElement('submit', null, 'Submit');
  123. $buttons[] = &HTML_QuickForm::createElement('reset', null, 'Reset');
  124. $buttons[] = &HTML_QuickForm::createElement('image', 'iimgTest', 'http://pear.php.net/gifs/pear-icon.gif');
  125. $buttons[] = &HTML_QuickForm::createElement('button', 'ibutTest', 'Test Button', array('onClick' => "alert('This is a test');"));
  126. $form->addGroup($buttons, null, null, ' ', false);
  127.  
  128.  
  129. // applies new filters to the element values
  130. $form->applyFilter('__ALL__', 'trim');
  131. // Adds some validation rules
  132. $form->addRule('itxtTest', 'Test Text is a required field', 'required');
  133. $form->addRule('itxaTest', 'Test TextArea is a required field', 'required');
  134. $form->addRule('itxaTest', 'Test TextArea must be at least 5 characters', 'minlength', 5);
  135. $form->addRule('ipwdTest', 'Password must be between 8 to 10 characters', 'rangelength', array(8, 10));
  136.  
  137. // Tries to validate the form
  138. if ($form->validate()) {
  139.     // Form is validated, then processes the data
  140.     $form->freeze();
  141.     $form->process('myProcess', false);
  142.     echo "\n<HR>\n";
  143. }
  144.  
  145. // Process callback
  146. function myProcess($values)
  147. {
  148.     echo '<pre>';
  149.     var_dump($values);
  150.     echo '</pre>';
  151. }
  152.  
  153. $form->display();
  154. ?>
  155.