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

Source for file SmartyDynamic_example.php

Documentation is available at SmartyDynamic_example.php

  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.4 2004/10/15 20:31:00 ths 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'array('Test Text''note' => 'Note for Testtext element.'));
  32.  
  33. $form->addElement('textarea''itxaTest''Test TextArea''cols="40" rows="2"');
  34.  
  35. // will be later assigned to style green
  36. $form->addElement('password''ipwdTest''Test Password');
  37. $select =$form->addElement(
  38.     'select',
  39.     'iselTest',
  40.     array('Test Select''note' => 'We recommend to check at least two categories!'),
  41.     array('A'=>'A * * * * (luxory)''B'=>'B * * *','C'=>'C * *','D'=>'D * (simple)')
  42.  );
  43. $select->setSize(4);
  44. $select->setMultiple(true);
  45.  
  46. $form->addElement('submit''isubTest''Test Submit');
  47.  
  48. $form->addElement('header''''Grouped Elements');
  49.  
  50. $checkbox[&HTML_QuickForm::createElement('checkbox''A'null'A');
  51. $checkbox[&HTML_QuickForm::createElement('checkbox''B'null'B');
  52. $checkbox[&HTML_QuickForm::createElement('checkbox''C'null'C');
  53. $checkbox[&HTML_QuickForm::createElement('checkbox''D'null'D');
  54. $form->addGroup($checkbox'ichkABCD''ABCD'array('&nbsp;''<br />'));
  55.  
  56. // will be later assigned to style fancygroup
  57. $radio[&HTML_QuickForm::createElement('radio'nullnull'Yes''Y');
  58. $radio[&HTML_QuickForm::createElement('radio'nullnull'No''N');
  59. $form->addGroup($radio'iradYesNo''Yes/No');
  60.  
  61. // will be later assigned to style fancygroup
  62. $name['first'&HTML_QuickForm::createElement('text''first''First:');
  63. $name['first']->setSize(20);
  64. $name['last'&HTML_QuickForm::createElement('text''last''Last:');
  65. $name['last']->setSize(30);
  66. $form->addGroup($name'name''Name');
  67.  
  68. // add some 'required' rules to show "stars" and (possible) errors...
  69. $form->addRule('itxtTest''Test Text is a required field''required');
  70. $form->addRule('itxaTest''Test TextArea is a required field''required');
  71. $form->addGroupRule('iradYesNo''Check Yes or No''required');
  72. $form->addGroupRule('name'array('last' => array(array('Last name is required''required'))));
  73.  
  74. // try to validate the form
  75. if ($form->validate()) {
  76.     $form->freeze();
  77. }
  78.  
  79. $renderer =new HTML_QuickForm_Renderer_Array(truetrue);
  80.  
  81. // give some elements aditional style informations
  82. $renderer->setElementStyle(array(
  83.     'ipwdTest'  => 'green',
  84.     'iradYesNo' => 'fancygroup',
  85.     'name'      => 'fancygroup'
  86. ));
  87.  
  88. $form->accept($renderer);
  89.  
  90. // setup a template object
  91. $tpl =new Smarty;
  92. $tpl->template_dir = './templates';
  93. $tpl->compile_dir  = './templates';
  94.  
  95. // assign array with form data
  96. $tpl->assign('form'$renderer->toArray());
  97.  
  98. // capture the array stucture
  99. // (only for showing in sample template)
  100. print_r($renderer->toArray());
  101. $tpl->assign('dynamic_array'ob_get_contents());
  102.  
  103. // render and display the template
  104. $tpl->display('smarty-dynamic.tpl');
  105.  
  106. ?>

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