Jump to content

HTML_QuickForm2 Doesn't Pass Validation


RonInNewYork

Recommended Posts

And I'm not sure why. Here's the code:

<?php
function displayPageHeader($pageTitle, $memberArea=false) {

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  <head>
    <title>QuickForm2</title>
    <link rel="stylesheet" type="text/css" href="common.css" />
<style type="text/css">
.error {background: #D33; color:white; padding: 0.2em;}
th {text-align: left; background-color: #bbb;}
th, td {padding: 0.4em;}
tr.alt td{background:#ddd;}
fieldset {border:none;}
ol {list-style:none;}
input, select, textarea {float: none; margin:1em 0 0 0; width:auto;}
div.element {float:right; width:57%;}
div.element label {display:inline; float:none}

</style>
  </head>
  <body>
<h1><?php echo $pageTitle?></h1>
<?php
}

// Load the main class
require_once 'HTML/QuickForm2.php';
require_once 'HTML/QuickForm2/Rule/Required.php';
require_once 'HTML/QuickForm2/Rule/Regex.php';
// Instantiate the HTML_QuickForm2 object
displayPageHeader("QuickForm2");
$form = new HTML_QuickForm2('tutorial', "GET","","", null, true);

// Set defaults for the form elements
$form->addDataSource(new HTML_QuickForm2_DataSource_Array(array(
    'name' => 'Joe User'
)));

// Add some elements to the form
$fieldset = $form->addElement('fieldset')->setLabel('QuickForm2 tutorial example');
$firstName = $fieldset->addElement('text', 'First Name', array('size' => 50, 'maxlength' => 255))
                 ->setLabel('First Name:');
$lastName = $fieldset->addElement('text', 'Last Name', array('size' => 50, 'maxlength' => 255))
                 ->setLabel('Last Name:');
$email= $fieldset->addElement('text', 'email',array('size'=>  30, 'maxlength'=> 30))->setLabel('Enter your email address:');
$fieldset->addElement('submit', null, array('value' => 'Send!'));

// Define filters and validation rules
$firstName->addFilter('trim');
$firstName->addRule('required', 'Please enter your first name');
$lastName->addFilter('trim');
$lastName->addRule('required', 'Please enter your last name');
$email->addRule('required');
$email->addRule(new HTML_QuickForm2_Rule_Regex(
    $email, 'Email should be in proper email format: person@example.com', '/^(?!(??:\x22?\x5C[\x00-\x7E]\x22?)|(?:\x22?[^\x5C\x22]\x22?)){255,})(?!(??:\x22?\x5C[\x00-\x7E]\x22?)|(?:\x22?[^\x5C\x22]\x22?)){65,}@)(??:[\x21\x23-\x27\x2A\x2B\x2D\x2F-\x39\x3D\x3F\x5E-\x7E]+)|(?:\x22(?:[\x01-\x08\x0B\x0C\x0E-\x1F\x21\x23-\x5B\x5D-\x7F]|(?:\x5C[\x00-\x7F]))*\x22))(?:\.(??:[\x21\x23-\x27\x2A\x2B\x2D\x2F-\x39\x3D\x3F\x5E-\x7E]+)|(?:\x22(?:[\x01-\x08\x0B\x0C\x0E-\x1F\x21\x23-\x5B\x5D-\x7F]|(?:\x5C[\x00-\x7F]))*\x22)))*@(??:(?!.*[^.]{64,})(??:(?:xn--)?[a-z0-9]+(?:-[a-z0-9]+)*\.){1,126}){1,}(??:[a-z][a-z0-9]*)|(??:xn--)[a-z0-9]+))(?:-[a-z0-9]+)*)|(?:\[(??:IPv6:(??:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){7})|(??!(?:.*[a-f0-9][:\]]){7,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?:?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?)))|(??:IPv6:(??:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){5}|(??!(?:.*[a-f0-9]{5,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3})?:?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3}?)))?(??:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))(?:\.(??:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))){3}))\]))$/iD'));


// Try to validate a form
if ($form->validate()) {
    echo '<h1>Hello, ' . htmlspecialchars($name->getValue()) . '!</h1>';
    exit;
}
else

// Output the form
?>
<div style="width:25em;">
<?php
echo $form;
?>
</div>

</body>
</html>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.