Template-based renderers with QuickForm_Controller? (PART II)
| From: | Maxim Volkov | Date: | Tue, 06 Apr 2004 18:13:31 +0000 |
| Subject: | Template-based renderers with QuickForm_Controller? (PART II) | ||
| Groups: | php.pear.general | ||
| Request: | Send a blank email to pear-general+get-11676@lists.php.net to get a copy of this message | ||
Here is a whole script where Template Smarty Renderer and
QuickForm_Controller are intended to work together:
------------------------------------------------------------------------
----------------------------------------
<?php
/**
* Example 2 for HTML_QuickForm_Controller: Wizard
*
* $Id: wizard.php,v 1.2 2003/10/02 12:49:38 avb Exp $
*/
require_once 'HTML/QuickForm/Controller.php';
// Load some default action handlers
require_once 'HTML/QuickForm/Action/Next.php';
require_once 'HTML/QuickForm/Action/Back.php';
require_once 'HTML/QuickForm/Action/Jump.php';
require_once 'HTML/QuickForm/Action/Display.php';
// Start the session, form-page values will be kept there
session_start();
define ('TEMPLATE_DIR',
'/export/websites/www.bellsmbsurvey.com/html/template/');
define ('TEMPLATE_PAGE1', 't1.html');
define ('TEMPLATE_PAGE2', 't2.html');
define ('TEMPLATE_PAGE3', 't3.html');
class MyDisplay extends HTML_QuickForm_Action_Display
{
function _renderForm(&$page)
{
include_once 'HTML/QuickForm.php';
include_once 'HTML/QuickForm/Renderer/ArraySmarty.php';
include_once 'Smarty.class.php';
# setup a template object
$tpl =& new Smarty;
$tpl->template_dir = TEMPLATE_DIR;
$tpl->compile_dir = TEMPLATE_DIR;
$renderer =& new HTML_QuickForm_Renderer_ArraySmarty($tpl);
$renderer->setRequiredTemplate(
'{if $error}
<font color="red">{$label}</font>
{else}
{$label}
{/if}'
);
$renderer->setErrorTemplate('');
# assign array with form data
$tpl->assign('form', $renderer->toArray());
# capture the array structure
ob_start();
print_r($renderer->toArray());
$tpl->assign('static_array', ob_get_contents());
ob_end_clean();
// $tpl->setCallbackFunction('buttonName', array(&$page,
'getButtonName'));
# Get content of the page $content = $tpl->get();
$tpl->display(TEMPLATE_DIR . TEMPLATE_PAGE1);
$page->display();
}
}
class PageFirst extends HTML_QuickForm_Page
{
function buildForm()
{
$this->_formBuilt = true;
$this->addElement('header', null, 'Wizard page 1 of 3');
$radio[] = &$this->createElement('radio', null, null, 'Yes',
'Y');
$radio[] = &$this->createElement('radio', null, null, 'No',
'N');
$this->addGroup($radio, 'iradYesNo', 'Are you absolutely
sure?');
$this->addElement('submit', $this->getButtonName('next'),
'Next >>');
$this->addRule('iradYesNo', 'Check Yes or No', 'required');
$this->setDefaultAction('next');
}
}
class PageSecond extends HTML_QuickForm_Page
{
function buildForm()
{
$this->_formBuilt = true;
$this->addElement('header', null, 'Wizard page 2 of 3');
$name['last'] = &$this->createElement('text', 'last',
null,
array('size' => 30));
$name['first'] = &$this->createElement('text', 'first',
null,
array('size' => 20));
$this->addGroup($name, 'name', 'Name (last, first):',
', ');
$prevnext[] =& $this->createElement('submit',
$this->getButtonName('back'), '<< Back');
$prevnext[] =& $this->createElement('submit',
$this->getButtonName('next'), 'Next >>');
$this->addGroup($prevnext, null, '', ' ', false);
$this->addGroupRule('name', array('last' => array(array('Last
name is required', 'required'))));
$this->setDefaultAction('next');
}
}
class PageThird extends HTML_QuickForm_Page
{
function buildForm()
{
$this->_formBuilt = true;
$this->addElement('header', null, 'Wizard page 3 of 3');
$this->addElement('textarea', 'itxaTest', 'Parting
words:',
array('rows' => 5, 'cols' => 40));
$prevnext[] =& $this->createElement('submit',
$this->getButtonName('back'), '<< Back');
$prevnext[] =& $this->createElement('submit',
$this->getButtonName('next'), 'Finish');
$this->addGroup($prevnext, null, '', ' ', false);
$this->addRule('itxaTest', 'Say something!', 'required');
$this->setDefaultAction('next');
}
}
class ActionProcess extends HTML_QuickForm_Action
{
function perform(&$page, $actionName)
{
echo "Submit successful!<br>\n<pre>\n";
var_dump($page->controller->exportValues());
echo "\n</pre>\n";
}
}
$wizard =& new HTML_QuickForm_Controller('Wizard');
$wizard->addPage(new PageFirst ('page1'));
$wizard->addPage(new PageSecond('page2'));
$wizard->addPage(new PageThird ('page3'));
// We actually add these handlers here for the sake of example
// They can be automatically loaded and added by the controller
# $wizard->addAction('display', new HTML_QuickForm_Action_Display() );
$wizard->addAction('display', new MyDisplay() );
$wizard->addAction('next', new HTML_QuickForm_Action_Next());
$wizard->addAction('back', new HTML_QuickForm_Action_Back());
$wizard->addAction('jump', new HTML_QuickForm_Action_Jump());
// This is the action we should always define ourselves
$wizard->addAction('process', new ActionProcess());
$wizard->run();
?>