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

  1. <?php
  2. /**
  3.  * Example of usage for HTML_QuickForm with Smarty renderer
  4.  *
  5.  * @author Bertrand Mansion <bmansion@mamasam.com>
  6.  * @author Thomas Schulz <ths@4bconsult.de>
  7.  *
  8.  * $Id: SmartyStatic_example.php,v 1.2 2003/05/15 19:31:54 avb Exp $ 
  9.  */
  10.  
  11. require_once 'HTML/QuickForm.php';
  12. require_once 'HTML/QuickForm/Renderer/ArraySmarty.php';
  13. // fix this if your Smarty is somewhere else
  14. require_once 'Smarty.class.php';
  15.  
  16. // Form name will be used to find the placeholders.
  17.  
  18. $form = new HTML_QuickForm('form', 'POST');
  19.  
  20. // Fills with some defaults values
  21.  
  22. $defaultValues['company']  = 'Mamasam';
  23. $defaultValues['country']  = array();
  24. $defaultValues['name']     = array('first'=>'Bertrand', 'last'=>'Mansion');
  25. $defaultValues['phone']    = array('513', '123', '4567');
  26. $form->setDefaults($defaultValues);
  27.  
  28. // Hidden
  29.  
  30. $form->addElement('hidden', 'session', '1234567890');
  31.  
  32. // Personal information
  33.  
  34. $form->addElement('header', 'personal', 'Personal Information');
  35.  
  36. $form->addElement('hidden', 'ihidTest', 'hiddenField');
  37. $form->addElement('text', 'email', 'Your email:');
  38. $form->addElement('password', 'pass', 'Your password:', 'size=10');
  39. $name['last'] = &HTML_QuickForm::createElement('text', 'first', 'First', 'size=10');
  40. $name['first'] = &HTML_QuickForm::createElement('text', 'last', 'Last', 'size=10');
  41. $form->addGroup($name, 'name', 'Name:', ', ');
  42. $areaCode = &HTML_QuickForm::createElement('text', '', null,'size=4 maxlength=3');
  43. $phoneNo1 = &HTML_QuickForm::createElement('text', '', null, 'size=4 maxlength=3');
  44. $phoneNo2 = &HTML_QuickForm::createElement('text', '', null, 'size=5 maxlength=4');
  45. $form->addGroup(array($areaCode, $phoneNo1, $phoneNo2), 'phone', 'Telephone:', '-');
  46.  
  47. // Company information
  48.  
  49. $form->addElement('header', 'company_info', 'Company Information');
  50.  
  51. $form->addElement('text', 'company', 'Company:', 'size=20');
  52.  
  53. $str[] = &HTML_QuickForm::createElement('text', '', null, 'size=20');
  54. $str[] = &HTML_QuickForm::createElement('text', '', null, 'size=20');
  55. $form->addGroup($str, 'street', 'Street:', '<br />');
  56.  
  57. $addr['zip'] = &HTML_QuickForm::createElement('text', 'zip', 'Zip', 'size=6 maxlength=10');
  58. $addr['city'] = &HTML_QuickForm::createElement('text', 'city', 'City', 'size=15');
  59. $form->addGroup($addr, 'address', 'Zip, city:');
  60.  
  61. $select = array('' => 'Please select...', 'AU' => 'Australia', 'FR' => 'France', 'DE' => 'Germany', 'IT' => 'Italy');
  62. $form->addElement('select', 'country', 'Country:', $select);
  63.  
  64. $checkbox[] = &HTML_QuickForm::createElement('checkbox', 'A', null, 'A');
  65. $checkbox[] = &HTML_QuickForm::createElement('checkbox', 'B', null, 'B');
  66. $checkbox[] = &HTML_QuickForm::createElement('checkbox', 'C', null, 'C');
  67. $checkbox[] = &HTML_QuickForm::createElement('checkbox', 'D', null, 'D');
  68. $form->addGroup($checkbox, 'destination', 'Destination:', array(' ', '<br />'));
  69.  
  70. // Other elements
  71.  
  72. $form->addElement('checkbox', 'news', '', " Check this box if you don't want to receive our newsletter.");
  73.  
  74. $form->addElement('reset', 'reset', 'Reset');
  75. $form->addElement('submit', 'submit', 'Register');
  76.  
  77. // Adds some validation rules
  78.  
  79. $form->addRule('email', 'Email address is required', 'required');
  80. $form->addGroupRule('name', 'Name is required', 'required');
  81. $form->addRule('pass', 'Password must be between 8 to 10 characters', 'rangelength', '8,10');
  82. $form->addRule('country', 'Country is a required field', 'required');
  83. $form->addGroupRule('destination', 'Please check at least two boxes', 'required', null, 2);
  84. $form->addGroupRule('phone', 'Please fill all phone fields', 'required');
  85. $form->addGroupRule('phone', 'Values must be numeric', 'numeric');
  86.  
  87. $AddrRules['zip'][0] = array('Zip code is required', 'required');
  88. $AddrRules['zip'][1] = array('Zip code is numeric only', 'numeric');
  89. $AddrRules['city'][0] = array('City is required', 'required');
  90. $AddrRules['city'][1] = array('City is letters only', 'lettersonly');
  91. $form->addGroupRule('address', $AddrRules);
  92.  
  93. // Tries to validate the form
  94. if ($form->validate()) {
  95.     // Form is validated, then freezes the data
  96.     $form->freeze();
  97. }
  98.  
  99. // setup a template object
  100. $tpl =& new Smarty;
  101. $tpl->template_dir = './templates';
  102. $tpl->compile_dir  = './templates';
  103.  
  104. $renderer =& new HTML_QuickForm_Renderer_ArraySmarty($tpl);
  105.  
  106. $renderer->setRequiredTemplate(
  107.    '{if $error}
  108.         <font color="red">{$label|upper}</font>
  109.     {else}
  110.         {$label}
  111.         {if $required}
  112.             <font color="red" size="1">*</font>
  113.         {/if}
  114.     {/if}'
  115.     );
  116.  
  117. $renderer->setErrorTemplate(
  118.    '{if $error}
  119.         <font color="orange" size="1">{$error}</font><br />
  120.     {/if}{$html}'
  121.     );
  122.  
  123. $form->accept($renderer);
  124.  
  125. // assign array with form data
  126. $tpl->assign('form', $renderer->toArray());
  127.  
  128. // capture the array stucture
  129. ob_start();
  130. print_r($renderer->toArray());
  131. $tpl->assign('static_array', ob_get_contents());
  132. ob_end_clean();
  133.  
  134. // render and display the template
  135. $tpl->display('smarty-static.tpl');
  136.  
  137. ?>
  138.