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

  1. <?php
  2. /**
  3.  * Example of usage for HTML_QuickForm Array renderer with Smarty template engine
  4.  *
  5.  * @author Thomas Schulz <ths@4bconsult.de>
  6.  * @author Alexey Borzov <borz_off@cs.msu.su>
  7.  *
  8.  * $Id: SmartyDynamic_example.php,v 1.2 2003/04/30 19:23:35 avb Exp $ 
  9.  */
  10.  
  11. require_once 'HTML/QuickForm.php';
  12. require_once 'HTML/QuickForm/Renderer/Array.php';
  13. // fix this if your Smarty is somewhere else
  14. require_once 'Smarty.class.php';
  15.  
  16. $form = new HTML_QuickForm('frmTest', 'post');
  17.  
  18. $form->setDefaults(array(
  19.     'itxtTest'  => 'Test Text Box',
  20.     'itxaTest'  => 'Hello World',
  21.     'iselTest'  => array('B', 'C'),
  22.     'name'      => array('first' => 'Thomas', 'last' => 'Schulz'),
  23.     'iradYesNo' => 'Y',
  24.     'ichkABCD'  => array('A'=>true,'D'=>true)
  25. ));
  26.  
  27. $form->addElement('header', '', 'Normal Elements');
  28.  
  29. $form->addElement('hidden', 'ihidTest', 'hiddenField');
  30.  
  31. $form->addElement('text', 'itxtTest', 'Test Text');
  32.  
  33. $form->addElement('textarea', 'itxaTest', 'Test TextArea');
  34.  
  35. // will be later assigned to style green
  36. $form->addElement('password', 'ipwdTest', 'Test Password');
  37. $select =& $form->addElement('select', 'iselTest', 'Test Select', array('A'=>'A', 'B'=>'B','C'=>'C','D'=>'D'));
  38. $select->setSize(5);
  39. $select->setMultiple(true);
  40.  
  41. $form->addElement('submit', 'isubTest', 'Test Submit');
  42.  
  43. $form->addElement('header', '', 'Grouped Elements');
  44.  
  45. $checkbox[] = &HTML_QuickForm::createElement('checkbox', 'A', null, 'A');
  46. $checkbox[] = &HTML_QuickForm::createElement('checkbox', 'B', null, 'B');
  47. $checkbox[] = &HTML_QuickForm::createElement('checkbox', 'C', null, 'C');
  48. $checkbox[] = &HTML_QuickForm::createElement('checkbox', 'D', null, 'D');
  49. $form->addGroup($checkbox, 'ichkABCD', 'ABCD', array(' ', '<br />'));
  50.  
  51. // will be later assigned to style fancygroup
  52. $radio[] = &HTML_QuickForm::createElement('radio', null, null, 'Yes', 'Y');
  53. $radio[] = &HTML_QuickForm::createElement('radio', null, null, 'No', 'N');
  54. $form->addGroup($radio, 'iradYesNo', 'Yes/No');
  55.  
  56. // will be later assigned to style fancygroup
  57. $name['first'] = &HTML_QuickForm::createElement('text', 'first', 'First:');
  58. $name['first']->setSize(20);
  59. $name['last'] = &HTML_QuickForm::createElement('text', 'last', 'Last:');
  60. $name['last']->setSize(30);
  61. $form->addGroup($name, 'name', 'Name');
  62.  
  63. // add some 'required' rules to show "stars" and (possible) errors...
  64. $form->addRule('itxtTest', 'Test Text is a required field', 'required');
  65. $form->addRule('itxaTest', 'Test TextArea is a required field', 'required');
  66. $form->addGroupRule('iradYesNo', 'Check Yes or No', 'required');
  67. $form->addGroupRule('name', array('last' => array(array('Last name is required', 'required'))));
  68.  
  69. // try to validate the form
  70. if ($form->validate()) {
  71.     $form->freeze();
  72. }
  73.  
  74. $renderer =& new HTML_QuickForm_Renderer_Array(true);
  75.  
  76. // give some elements aditional style informations
  77. $renderer->setElementStyle(array(
  78.     'ipwdTest'  => 'green',
  79.     'iradYesNo' => 'fancygroup',
  80.     'name'      => 'fancygroup'
  81. ));
  82.  
  83. $form->accept($renderer);
  84.  
  85. // setup a template object
  86. $tpl =& new Smarty;
  87. $tpl->template_dir = './templates';
  88. $tpl->compile_dir  = './templates';
  89.  
  90. // assign array with form data
  91. $tpl->assign('form', $renderer->toArray());
  92.  
  93. // capture the array stucture 
  94. // (only for showing in sample template)
  95. ob_start();
  96. print_r($renderer->toArray());
  97. $tpl->assign('dynamic_array', ob_get_contents());
  98. ob_end_clean();
  99.  
  100. // render and display the template
  101. $tpl->display('smarty-dynamic.tpl');
  102.  
  103. ?>
  104.