Women in Technology

Hear us Roar



Article:
  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">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html"
charset="UTF-8">
<title>Testing PEAR HTML_QuickForm</title>
</head>
<body>


<?php
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',
'Clear');


$form->addElement('submit', 'btnSubmit',
'Submit');


$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->freeze();
$form->process("process_data", false);
}
else {
$form->display();
}


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;
}
}


?>
</body>
</html>


Svetoslav
http://devquickref.com