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

Source for file groups.php

Documentation is available at groups.php

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

Documentation generated on Mon, 11 Mar 2019 14:46:24 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.