Source code in QfSmartyOverlib.php

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) 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: @(#)QfSmartyOverlib.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";
    require_once 'HTML/QuickForm/Renderer/ArraySmarty.php';
    require_once 'Smarty.class.php';

    $form = new HTML_QuickForm('QfDemo', 'get');
    $form->addElement('header', 'DemoHeader', 'Quickform 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;
    }

    // Create the template object
    $tpl =& new Smarty;
    $tpl->template_dir = '.';

    // 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
    // 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 "<pre>";var_dump($renderer->toArray());echo "</pre>";

?>

Return to this tutorial index.