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

  1. <?
  2. /**
  3.  * Example of usage for HTML_QuickForm Object renderer 
  4.  * with Flexy template engine and static template
  5.  *
  6.  * @author Ron McClain <mixtli@cats.ucsc.edu>
  7.  *
  8.  * $Id: FlexyStatic_example.php,v 1.3 2003/11/03 12:55:53 avb Exp $ 
  9.  */
  10.  
  11. require_once('HTML/Template/Flexy.php');
  12. require_once('HTML/QuickForm.php');
  13. require_once('HTML/QuickForm/Renderer/ObjectFlexy.php');
  14.  
  15. function myProcess($values)
  16. {
  17.     echo "<pre>";
  18.     var_dump($values);
  19.     echo "</pre>";
  20. }
  21.  
  22. $form = new HTML_QuickForm('form', 'POST');
  23.  
  24. // Fills with some defaults values
  25.  
  26. $defaultValues['company']  = 'Devils son in law';
  27. $defaultValues['country']  = array();
  28. $defaultValues['name']     = array('first'=>'Petey', 'last'=>'Wheatstraw');
  29. $defaultValues['phone']    = array('513', '123', '4567');
  30. $form->setDefaults($defaultValues);
  31.  
  32. // Hidden
  33.  
  34. $form->addElement('hidden', 'session', '1234567890');
  35.  
  36. // Personal information
  37.  
  38. $form->addElement('header', 'personal', 'Personal Information');
  39.  
  40. $form->addElement('hidden', 'ihidTest', 'hiddenField');
  41. $form->addElement('text', 'email', 'Your email:');
  42. $form->addElement('password', 'pass', 'Your password:', 'size=10');
  43. $name['last'] = &HTML_QuickForm::createElement('text', 'first', 'First',
  44. 'size=10');
  45. $name['first'] = &HTML_QuickForm::createElement('text', 'last', 'Last',
  46. 'size=10');
  47. $form->addGroup($name, 'name', 'Name:', ', ');
  48. $areaCode = &HTML_QuickForm::createElement('text', '', null,'size=4
  49. maxlength=3');
  50. $phoneNo1 = &HTML_QuickForm::createElement('text', '', null, 'size=4
  51. maxlength=3');
  52. $phoneNo2 = &HTML_QuickForm::createElement('text', '', null, 'size=5
  53. maxlength=4');
  54. $form->addGroup(array($areaCode, $phoneNo1, $phoneNo2), 'phone',
  55. 'Telephone:', '-');
  56.  
  57. // Company information
  58.  
  59. $form->addElement('header', 'company_info', 'Company Information');
  60.  
  61. $form->addElement('text', 'company', 'Company:', 'size=20');
  62.  
  63. $str[] = &HTML_QuickForm::createElement('text', '', null, 'size=20');
  64. $str[] = &HTML_QuickForm::createElement('text', '', null, 'size=20');
  65. $form->addGroup($str, 'street', 'Street:', '<br />');
  66.  
  67. $addr['zip'] = &HTML_QuickForm::createElement('text', 'zip', 'Zip', 'size=6
  68. maxlength=10');
  69. $addr['city'] = &HTML_QuickForm::createElement('text', 'city', 'City',
  70. 'size=15');
  71. $form->addGroup($addr, 'address', 'Zip, city:');
  72.  
  73. $select = array('' => 'Please select...', 'AU' => 'Australia', 'FR' =>
  74. 'France', 'DE' => 'Germany', 'IT' => 'Italy');
  75. $form->addElement('select', 'country', 'Country:', $select);
  76.  
  77. $checkbox[] = &HTML_QuickForm::createElement('checkbox', 'A', null, 'A');
  78. $checkbox[] = &HTML_QuickForm::createElement('checkbox', 'B', null, 'B');
  79. $checkbox[] = &HTML_QuickForm::createElement('checkbox', 'C', null, 'C');
  80. $checkbox[] = &HTML_QuickForm::createElement('checkbox', 'D', null, 'D');
  81. $form->addGroup($checkbox, 'destination', 'Destination:', array(' ',
  82. '<br />'));
  83.  
  84. // Other elements
  85.  
  86. $form->addElement('checkbox', 'news', '', " Check this box if you don't want
  87. to receive our newsletter.");
  88.  
  89. $form->addElement('reset', 'reset', 'Reset');
  90. $form->addElement('submit', 'submit', 'Register');
  91.  
  92. // Adds some validation rules
  93.  
  94. $form->addRule('email', 'Email address is required', 'required');
  95. $form->addGroupRule('name', 'Name is required', 'required');
  96. $form->addRule('pass', 'Password must be between 8 to 10 characters',
  97. 'rangelength', array(8, 10),'client');
  98. $form->addRule('country', 'Country is a required field', 'required');
  99. $form->addGroupRule('destination', 'Please check at least two boxes',
  100. 'required', null, 2);
  101. $form->addGroupRule('phone', 'Please fill all phone fields', 'required');
  102. $form->addGroupRule('phone', 'Values must be numeric', 'numeric');
  103.  
  104. $AddrRules['zip'][0] = array('Zip code is required', 'required');
  105. $AddrRules['zip'][1] = array('Zip code is numeric only', 'numeric');
  106. $AddrRules['city'][0] = array('City is required', 'required');
  107. $AddrRules['city'][1] = array('City is letters only', 'lettersonly');
  108. $form->addGroupRule('address', $AddrRules);
  109.  
  110. // Tries to validate the form
  111. if ($form->validate()) {
  112.     // Form is validated, then freezes the data
  113.     $form->freeze();
  114.     $form->process('myProcess',  false);
  115.     echo "\n<hr>\n";
  116.  
  117. // setup a template object
  118. $options = &PEAR::getStaticProperty('HTML_Template_Flexy','options');
  119. $options = array(
  120.     'templateDir' => './templates',
  121.     'compileDir' => './templates/build',
  122.     'forceCompile' => 1,
  123.     'debug' => 0,
  124.     'local' => 'en'
  125. );
  126.  
  127. $template = new HTML_Template_Flexy($options);
  128.  
  129. $renderer =& new HTML_QuickForm_Renderer_ObjectFlexy(&$template);
  130. $renderer->setLabelTemplate("label.html");
  131. $renderer->setHtmlTemplate("html.html");
  132.  
  133. $form->accept($renderer);
  134.  
  135. $view = new StdClass;
  136. $view->form = $renderer->toObject();
  137.  
  138. $template->compile("flexy-static.html");
  139. // capture the array stucture
  140. ob_start();
  141. print_r($view->form);
  142. $view->static_object =  ob_get_contents();
  143. ob_end_clean();
  144.  
  145. // render and display the template
  146. $template->outputObject($view);
  147. ?>