To see how this is used return to this tutorial index.
<?php // Integration of Quickform with Smarty. // This provides the 'logic', the html template is in QfSmarty.tpl // Most of this is the same as in the previous examples, the differences are at the end. // Copyright (c) 2005, 2013 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: @(#)QfSmarty.php 1.4 10/17/13 21:55:50 // Stop warnings due to HTML_Quickform being stuck at PHP4 if($ini_val = ini_get('error_reporting')) ini_set('error_reporting', ($ini_val & ~(E_STRICT | E_DEPRECATED))); // Check that the title is valid: function CheckTitle($t) { global $Titles; return 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/QuickForm.php"; $form = new HTML_QuickForm('QfDemo', 'get'); $form->addElement('header', 'DemoHeader', 'Quickform with Smarty Demo'); $form->applyFilter('__ALL__', 'trim'); $Titles = array('Please Select', 'Mr', 'Mrs', 'Miss', 'Dr'); $form->addElement('select', 'Title', 'Title', $Titles); $form->addRule('Title', 'The title is required', 'required', null, 'client'); $form->addRule('Title', 'The title must be in the list', 'numeric', null, 'client'); $form->addRule('Title', 'Please select the title from the list', 'nonzero', null, 'client'); $form->applyFilter('Title', 'CheckTitle'); $form->addElement('text', 'FirstName', 'First name'); $form->addRule('FirstName', 'First name is required', 'required', null, 'client'); $form->registerRule('checkfn', 'callback', 'checkFNfunc'); $form->addRule('FirstName', 'First name is not in approved list', 'checkfn'); $form->addElement('text', 'LastName', 'Last name'); $form->addRule('LastName', 'Last name is required', 'required', null, 'client'); $form->addElement('text', 'Age', 'Age'); $form->addRule('Age', 'Age is required', 'required'); $form->addRule('Age', 'Age must be numeric', 'numeric', null, 'client'); $form->addElement('text', 'Telephone', 'Telephone number'); $form->addRule('Telephone', 'The phone number must start with 0, or international ones with +', 'regex', '/^[0+]/', 'client'); $form->addElement('reset', 'Clear', 'Clear'); $form->addElement('submit', 'Submit', '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->exportValues(); // print_r($res); 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 '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 $renderer =& new HTML_QuickForm_Renderer_ArraySmarty($tpl); // build the HTML for the form $form->accept($renderer); // assign array with form data $tpl->assign('FormData', $renderer->toArray()); // parse and display the template $tpl->display('QfSmarty.tpl'); // echo "<pre>";var_dump($renderer->toArray());echo "</pre>"; ?>
Return to this tutorial index.