Women in Technology

Hear us Roar

  Getting Started with PHP's HTML_QuickForm
Subject:   Feedback
Date:   2005-09-23 03:11:05
From:   Lordspace
Here is a modified version of the example1

it seems you can't addRule() in validation method to set up a rule that validates email address.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<meta http-equiv="Content-Type" content="text/html"
<title>Testing PEAR HTML_QuickForm</title>

require_once "HTML/QuickForm.php";

$form = new HTML_QuickForm('frmTest', 'get');

$form->addElement('header', 'hdrQuickformtest',
'QuickForm Example 1');
$form->addElement('text', 'txtName',
'What is your name?');

$form->addElement('text', 'txtEmail',
'What is your email?');

$form->addElement('advcheckbox', 'chkNewsletter',
'Receive newsletter?');

$form->addElement('reset', 'btnClear',

$form->addElement('submit', 'btnSubmit',

$form->addRule( 'txtName', "Please enter your name", "required" );
$form->addFormRule( "newcontact_validate" );
$form->applyFilter( '__ALL__', 'trim' );

if ( @$_REQUEST[ 'chkNewsletter' ] )
$form->addRule( 'txtEmail', "Please enter your email", "email" );

if ($form->validate()) {
# If the form validates, freeze and process the data
$form->process("process_data", false);
else {

function process_data ($values)
echo "


foreach ($values as $key=>$value) {
echo $key."=".$value."

echo "


// when a checkbox is selected validate email address otherwise no
function newcontact_validate( $fields )
# Set up array to store any errors
$errors = array();

# Carry out validation checks
if ("1" == $fields['chkNewsletter']) {
if (empty($fields['txtEmail'])) {
$errors['txtEmail'] =
"Please enter an email address for the newsletter";

# Return the errors if we have any, or TRUE if not
if (empty($errors)) {
return TRUE;
else {
return $errors;