To see how this is used return to this tutorial index.
<?php
// Quickform, Smarty and Overlib all at once. If you have not seen the previous examples, return to the index to do that first.
// Look at the template (QfSmartyOverlib.tpl) to see how overlib is used. There is no overlib related code in this file.
// Just the use of a different template file.
// Copyright (c) 2009, Parliament Hill Computers (www.phcomp.co.uk). Author: Alain D D Williams (addw@phcomp.co.uk).
// You may used this file as the basis your own (or organisation's/company's) project (under whatever
// licence that you see fit), but may not claim ownership or copyright of the substantially unmodified file.
// This file is made available in the hope that it is useful, there is no warranty at all, use at your own risk.
// SCCS: @(#)Qf2SmartyOverlib.php 1.3 12/29/09 14:12:37
// Check that the title is valid.
// Don't want 0 since that is the one that asks for the title to be selected
function CheckTitle($t)
{
global $Titles;
return $t > 0 && isset($Titles[$t]);
}
// Check that the first name is one of an approved list, callback function:
function checkFNfunc($name)
{
$OKlist = array('John', 'Bill');
return strcmp(array_search($name, $OKlist), '');
}
require_once "HTML/QuickForm2.php";
// You may need to add the 4th argument 'false' otherwise QF2 will look for a hidden
// input field _qf__$id ($id is 'QfDemo' here) to check if the form has
// been submitted. The existing Smarty templates may not push out these hidden fields.
// $form = new HTML_QuickForm2('QfDemo', 'get', null, false);
// This was an expectation that 'hidden' was a string, rather than QF2's array - the Smarty
// renderer fixes that.
$form = new HTML_QuickForm2('QfDemo', 'get');
foreach($_GET as &$gv)
if(is_string($vg)) // Avoid arrays
$gv = trim($gv);
$Titles = array('Please select', 'Mr', 'Mrs', 'Miss', 'Dr');
$title = $form->addElement('select', 'Title', null, array('label' => 'Title', 'options' => $Titles));
$title->addRule('required', 'A title must be set');
$title->addRule('regex', 'A title must be from the list', '/^\d+$/'); // numeric
$title->addRule('callback', 'A title must be from the list', 'CheckTitle');
$first = $form->addElement('text', 'FirstName', null, array('label' => 'First name'));
$first->addRule('required', 'First name is required');
$first->addRule('callback', 'First name is not in approved list', 'checkFNfunc');
$last = $form->addElement('text', 'LastName', null, array('label' => 'Last name'));
$last->addRule('required', 'Last name is required');
$age = $form->addElement('text', 'Age', array('maxlength' => 3, 'size' => 3), array('label' => 'Age'));
$age->addRule('required', 'Age is required');
$age->addRule('regex', 'Age must be numeric', '/^\d+$/');
$phone = $form->addElement('text', 'Telephone', null, array('label' => 'Telephone number'));
$phone->addRule('regex', 'The phone number must start with 0, or international ones with +',
'/^[0+]/');
$form->addElement('reset', 'Clear');
$form->addElement('submit', 'Submit', array('value' => 'Press to Submit'));
if ($form->validate()) {
# Get here when the form has been filled in and validation checks passed
// $form->freeze(); // If we want the form redisplayed in the way that the user entered it
// but you need to do another display()
$res = $form->getValue();
echo "Title = " . htmlspecialchars($res['Title']) . " = '" . $Titles[$res['Title']] . "'<br>\n";
echo "FirstName = " . htmlspecialchars($res['FirstName']) . "<br>\n";
echo "LastName = " . htmlspecialchars($res['LastName']) . "<br>\n";
echo "Age = " . htmlspecialchars($res['Age']) . "<br>\n";
echo "Telephone = " . htmlspecialchars($res['Telephone']) . "<br>\n";
exit;
}
// **** Differences from previous examples start here: ****
// require_once 'HTML/QuickForm/Renderer/ArraySmarty.php';
require_once 'HTML/QuickForm2/Renderer.php';
require_once 'Smarty.class.php';
// Create the template object
$tpl = new Smarty;
$tpl->template_dir = '.'; // Where the template files live
// Smarty 'compiles' the .tpl and creates a .php script to generate the html
// Next time smarty will use the .php without recompilation if the .tpl has not changed
// This will live in the templates_c directory.
// This templates_c/XXXXX.php is run to produce html
// We could cache this html but that would mean that we would not see error messages or other things
// that are dynamically genereated. Do not switch on caching.
// Create the renderer object
HTML_QuickForm2_Renderer::register('smarty', 'HTML_QuickForm2_Renderer_Smarty');
$renderer = HTML_QuickForm2_Renderer::factory('smarty');
// Generate styles/output compatible with the old renderer (for old templates):
$renderer->setOption('group_errors', true);
$renderer->setOption('old_compat', true);
// build the HTML for the form
$FormData = $form->render($renderer)->toArray();
// This is where the header is inserted directly into the array for smarty:
$FormData['header']['DemoHeader'] = 'Quickform & Overlib Demo';
$tpl->assign('FormData', $FormData);
// parse and display the template
// This line below is the only on the has changed from the previous example, ie
// just use a different template file that invokes overlib.
$tpl->display('QfSmartyOverlib.tpl');
// Comment this back in for debugging purposes:
// echo "<b>toArray()</b><pre>";var_dump($renderer->toArray());echo "</pre>";
// echo "<b>form</b><pre>";var_dump($form);echo "</pre>";
?>
Return to this tutorial index.