Source code in Qf2Valid.php

<?php
// This generates a simple quickform that validates form data, both client and server side.
// You should look at QfSimple.php before you try to understand this 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: @(#)Qf2Valid.php 1.4 10/21/13 15:08:34
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Quickform Demo</title>
<link rel="stylesheet" type="text/css" href="/DefaultStyle.css">
<link rel="stylesheet" type="text/css" href="Qf2Style.css">
</head>
<body>
<!-- A standard page header -->
<div style='position : absolute;text-align: left;top:10px;'>
<a href='/'><img src='/Images/kite_40_83_trans.gif' width="40" height="83" alt="Parliament Hill Computers Ltd" style="border-width: 0;"></a>
</div>
<div style='top:0px;margin:0px auto;text-align: center; font-size:40pt; '>
Simple Quickform
</div>
<p>
<?php
// 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 used in FirstName:
function checkFNfunc($name)
{
$OKlist = array('John', 'Bill'); // A very simple approved list
return strcmp(array_search($name, $OKlist), '');
}
require_once "HTML/QuickForm2.php";
// Create a GET form with the name QfDemo:
$form = new HTML_QuickForm2('QfDemo', 'get');
// addElement() adds a display line or an input field to the form.
// The first argument is a standard html type (text, checkbox, hidden, ...) or a custom
// type (header, group, ...). Other arguments depend on the type.
// $form->addElement('header', 'DemoHeader', 'Quickform Demo');
$form->addElement('hidden', 'DemoHeader')->setValue('Quickform Demo');
// Another way of doing this:
// $form->addElement('hidden', 'DemoHeader', array('value' =>'Quickform Demo'));
// When the form has been filled in, trim spaces from all fields:
$form->addRecursiveFilter('trim');
// Add a drop down select list:
$Titles = array('Please select', 'Mr', 'Mrs', 'Miss', 'Dr');
// $form->addElement('select', 'Title', 'Title', $Titles);
$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');
// Add a text field, the form element name is 'FirstName' with a title 'First name'
// $form->addElement('text', 'FirstName', 'First name');
$first = $form->addElement('text', 'FirstName', null, array('label' => 'First name'));
// $form->addRule('FirstName', 'First name is required', 'required', null, 'client');
$first->addRule('required', 'First name is required');
// The rule checkfn is to be implemented by calling checkFNfunc(), we apply that
// to the FirstName field - this is a server side only check
// $form->registerRule('checkfn', 'callback', 'checkFNfunc');
// $form->addRule('FirstName', 'First name is not in approved list', 'checkfn');
$first->addRule('callback', 'First name is not in approved list', 'checkFNfunc');
// A client side check is made that LastName has been given a value.
// All client side checks are repeated server side -- it is not safe to rely on client only checks
// $form->addElement('text', 'LastName', 'Last name');
$last = $form->addElement('text', 'LastName', null, array('label' => 'Last name'));
// $form->addRule('LastName', 'Last name is required', 'required', null, 'client');
$last->addRule('required', 'Last name is required');
// Age is required (must be filled in) and must be numeric.
// Specify the size - people don't get very old!
// $form->addElement('text', 'Age', 'Age');
// $form->addRule('Age', 'Age is required', 'required');
// $form->addRule('Age', 'Age must be numeric', 'numeric', null, 'client');
$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+$/');
// Telephone number is checked with a regular expression, but is not required:
// $form->addElement('text', 'Telephone', 'Telephone number');
// $form->addRule('Telephone', 'The phone number must start with 0, or international ones with +',
// 'regex', '/^[0+]/', 'client');
$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+]/');
// Standard buttons (no break space label to get alignment):
// $form->addElement('reset', 'Clear', 'Clear');
// $form->addElement('submit', 'Submit', 'Submit');
$form->addElement('reset', 'Clear');
$form->addElement('submit', 'Submit', array('value' => 'Press to Submit'));
// First time round this results in false, remember that, as is common in php, this
// script is used both to generate the form and to process the results.
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";
} else {
// $form->setDefaults(array('FirstName'=>'John', 'LastName'=>'Bloggs')); // only does something with empty form
$form->addDataSource(new HTML_QuickForm2_DataSource_Array(
array('FirstName'=>'John', 'LastName'=>'Bloggs'))); // only does something with empty form
echo $form;
}
?>
<p class="notes">
This is a simple form, fill in some values and press <i>Submit</i>.
<p>
There is some validation on this form:
<ul>
<li> Firstname: it must have a value; server side: the value 'Bill' or 'John'
<li> Lastname: it must have a value
<li> Age: it must have a value and be numeric
<li> Telephone: it must start '0' or '+'
</ul>
<p style="font-size: x-large; font-weight: bold;">
Return to this <em><a href='index.php'>tutorial index</a></em>.
</p>
</body>
</html>