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

  1. <?php
  2. /**
  3. * Examples of usage for grouped elements in HTML_QuickForm
  4. *
  5. * $Id: groups.php,v 1.1 2003/12/20 21:05:32 avb Exp $
  6. * @author      Bertrand Mansion <bmansion@mamasam.com>
  7. * @author      Alexey Borzov <avb@php.net>
  8. * @version     3.2
  9. */
  10.  
  11. require_once 'HTML/QuickForm.php';
  12.  
  13. $form =& new HTML_QuickForm('frmGroups');
  14. $form->setDefaults(array(
  15.     'id'        => array('lastname' => 'Mamasam', 'code' => '1234'),
  16.     'phoneNo'   => array('513', '123', '3456'),
  17.     'ichkABC'   => array('A'=>true)
  18. ));
  19.  
  20. $renderer =& $form->defaultRenderer();
  21.  
  22. // Setting templates for form and headers
  23. $renderer->setFormTemplate("<form{attributes}>\n<table width=\"450\" border=\"0\" cellpadding=\"3\" cellspacing=\"2\" bgcolor=\"#CCCC99\">\n{content}\n</table>\n</form>");
  24. $renderer->setHeaderTemplate("\t<tr>\n\t\t<td style=\"white-space:nowrap;background:#996;color:#ffc;\" align=\"left\" colspan=\"2\"><b>{header}</b></td>\n\t</tr>");
  25.  
  26. // Setting a special template for id element
  27. $renderer->setGroupTemplate('<table><tr>{content}</tr></table>', 'id');
  28. $renderer->setGroupElementTemplate('<td>{element}<br /><span style="font-size:10px;"><!-- BEGIN required --><span style="color: #f00">* </span><!-- END required --><span style="color:#996;">{label}</span></span></td>', 'id');
  29.  
  30.  
  31. $form->addElement('header', '', 'Tests on grouped elements');
  32.  
  33. // Creates a group of text inputs with templates
  34. $id['lastname'] = &HTML_QuickForm::createElement('text', 'lastname', 'Name', array('size' => 30));
  35. $id['code'] = &HTML_QuickForm::createElement('text', 'code', 'Code', array('size' => 5, 'maxlength' => 4));
  36. $form->addGroup($id, 'id', 'ID:', ', ');
  37.  
  38. // Add a complex rule for id element
  39. $form->addGroupRule('id', array(
  40.     'lastname' => array(
  41.         array('Name is required', 'required', null, 'client'),
  42.         array('Name is letters only', 'lettersonly', null, 'client')
  43.     ),
  44.     'code'     => array(
  45.         array('Code must be numeric', 'numeric', null, 'client')
  46.     )
  47. ));
  48.  
  49.  
  50. // Creates a group of text inputs
  51. $areaCode = &HTML_QuickForm::createElement('text', '', null, array('size' => 4, 'maxlength' => 3));
  52. $phoneNo1 = &HTML_QuickForm::createElement('text', '', null, array('size' => 4, 'maxlength' => 3));
  53. $phoneNo2 = &HTML_QuickForm::createElement('text', '', null, array('size' => 5, 'maxlength' => 4));
  54. $form->addGroup(array($areaCode, $phoneNo1, $phoneNo2), 'phoneNo', 'Telephone:', '-');
  55.  
  56. // Adds validation rules for groups
  57. $form->addGroupRule('phoneNo', 'Please fill all phone fields', 'required', null, 3, 'client');
  58. $form->addGroupRule('phoneNo', 'Values must be numeric', 'numeric', null, 3, 'client');
  59.  
  60. // Creates a checkboxes group using an array of separators
  61. $checkbox[] = &HTML_QuickForm::createElement('checkbox', 'A', null, 'A');
  62. $checkbox[] = &HTML_QuickForm::createElement('checkbox', 'B', null, 'B');
  63. $checkbox[] = &HTML_QuickForm::createElement('checkbox', 'C', null, 'C');
  64. $checkbox[] = &HTML_QuickForm::createElement('checkbox', 'D', null, 'D');
  65. $form->addGroup($checkbox, 'ichkABC', 'ABCD:', array(' ', '<br />'));
  66.  
  67. // At least one element is required
  68. $form->addGroupRule('ichkABC', 'Please check at least two boxes', 'required', null, 2, 'client', true);
  69.  
  70. // Creates a standard radio buttons group
  71. $radio[] = &HTML_QuickForm::createElement('radio', null, null, 'Yes', 'Y');
  72. $radio[] = &HTML_QuickForm::createElement('radio', null, null, 'No', 'N');
  73. $form->addGroup($radio,  'iradYesNo', 'Yes/No:');
  74.  
  75. // Validate the radio buttons
  76. $form->addRule('iradYesNo', 'Check Yes or No', 'required', null, 'client');
  77.  
  78. // Creates a group of buttons to be displayed at the bottom of the form
  79. $buttons[] =& $form->createElement('submit', null, 'Submit');
  80. $buttons[] =& $form->createElement('reset', null, 'Reset');
  81. $buttons[] =& $form->createElement('checkbox', 'clientSide', null, 'use client-side validation', array('checked' => 'checked', 'onclick' => "if (this.checked) {this.form.onsubmit = validate_" . $form->getAttribute('id') . ";} else {this.form.onsubmit = null;}"));
  82. $form->addGroup($buttons);
  83.  
  84.  
  85. // Tries to validate the form
  86. if ($form->validate()) {
  87.     // Form is validated, then processes the data
  88.     $form->freeze();
  89.     $form->process('var_dump');
  90.     echo "\n<HR>\n";
  91. }
  92. $form->display();
  93.  
  94. ?>
  95.