PHP Classes

File: examples/example.php

Recommend this page to a friend!
  Classes of Stefan Gabos   PHP Forms Generation and Validation Class   examples/example.php   Download  
File: examples/example.php
Role: Example script
Content type: text/plain
Description: example script
Class: PHP Forms Generation and Validation Class
Generate and validate HTML forms
Author: By
Last change: fixed some bugs
Date: 17 years ago
Size: 10,864 bytes
 

Contents

Class file image Download
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>PHP HTML Form Processor Class</title> </head> <body> <?php // create a custom function that compares a control's // submitted value with another value function valueCompare($controlValue, $valueToCompareTo) { // if values are not the same if ($controlValue != $valueToCompareTo) { // return false return false; } // return true if everything is ok return true; } // require the htmlform class require "../class.htmlform.php"; // instantiate the class $form = new HTMLForm("form", "post"); // specify the date to the datepicker class (a member of the Zebra PHP Framework) // if class is not found the execution will break when you try to instantiate a date control $form->datePickerPath = "../../datepicker/class.datepicker.php"; // add a label control to the form, having the name and id attributes set to "label_name", // being attached to the control having the name and id attributes set to "name", and // having as caption the text "Your name" // in the template file, you'll be able to call this control's output by using {controls.label_name} $form->add("label", "label_name", "name", "Your name"); // add a text box control to the form, having the name and id attributes set to "name" // notice the '&' - this is there in order for $obj to really point to the newly addded control in PHP4 // in the template file, you'll be able to call this control's output by using {controls.name} $obj = & $form->add("text", "name"); // make this control mandatory and in case of error, send the "Please enter your name" message to "e1" error block // in the template file, you'll be able to call the error messages sent to this error block by using {error.e1} // NOTE THAT YOU CAN SEND AS MANY ERROR MESSAGES AS YOU LIKE TO A SINGLE ERROR BLOCK. THERE'S NO NEED TO MAKE // ERROR BLOCKS FOR EACH CONTROL! $obj->setRule(array("mandatory"=>array("e1", "Please enter your name"))); // add a custom validation rule $obj->setRule(array("custom"=>array("valueCompare", "admin", "e1", "Enter 'admin' as the name"))); // add a label control to the form, having the name and id attributes set to "label_password", // being attached to the control having the name and id attributes set to "password", and // having as caption the text "Choose a password" // in the template file, you'll be able to call this control's output by using {controls.label_password} $form->add("label", "label_password", "password", "Choose a password"); // add a password control to the form, having the name and id attributes set to "password" // notice the '&' - this is there in order for $obj to really point to the newly addded control in PHP4 // in the template file, you'll be able to call this control's output by using {controls.password} $pwd = & $form->add("password", "password"); // make this control mandatory and, in case of error, send the "Password is required" message to "e2" error block // in the template file, you'll be able to call the error messages sent to this error block by using {error.e2} // NOTE THAT YOU CAN SEND AS MANY ERROR MESSAGES AS YOU LIKE TO A SINGLE ERROR BLOCK. THERE'S NO NEED TO MAKE // ERROR BLOCKS FOR EACH CONTROL! $pwd->setRule(array("mandatory"=>array("e2", "Password is required"))); // also, make the minimum allowed length for the password to be 6 characters and, in case of error, send the // "The minimum length for passwords is 6 characters" message to "e2" error block $pwd->setRule(array("minlength"=>array("6", "e2", "The minimum length for passwords is 6 characters"))); // add a label control to the form, having the name and id attributes set to "label_confirm_password", // being attached to the control having the name and id attributes set to "confirm", and // having as caption the text "Confirm password" // in the template file, you'll be able to call this control's output by using {controls.label_confirm_password} $form->add("label", "label_confirm_password", "password", "Confirm password"); // add a password control to the form, having the name and id attributes set to "confirm" // notice the '&' - this is there in order for $obj to really point to the newly addded control in PHP4 // in the template file, you'll be able to call this control's output by using {controls.confirm} $obj = & $form->add("password", "confirm"); // set a rule for this control - it's value must be equal to the value entered in the 'password' field and, in case of error // send the "Password not confirmed correctly" message to "e2" error block // NOTE THAT WE'RE SENDING THE MESSAGE TO AN ERROR BLOCK THAT MIGHT ALREADY CONTAIN ERROR MESSAGES! $obj->setRule(array("compare"=>array("password", "e2", "Password not confirmed correctly!"))); // add a label control to the form, having the name and id attributes set to "label_age", // being attached to the control having the name and id attributes set to "age", and // having as caption the text "Your age" // in the template file, you'll be able to call this control's output by using {controls.label_age} $form->add("label", "label_age", "age", "Your age"); // add a select control, having the name and id attributes set to "age" // notice the '&' - this is there in order for $obj to really point to the newly addded control in PHP4 // in the template file, you'll be able to call this control's output by using {controls.age} $obj = & $form->add("select", "age"); // add some options to the select control // NOTE THAT, IF THE "MULTIPLE" ATTRIBUTE IS NOT SET, THE FIRST OPTION WILL BE ALWAYS CONSIDERED AS THE // "NOTHING IS SELECTED" STATE OF THE CONTROL! $obj->addOptions(array("- select -", "Between 0 an 30", "Between 30 and 60", "Older than 60")); // make this control mandatory and, in case of error, send the "Password is required" message to "e2" error block // in the template file, you'll be able to call the error messages sent to this error block by using {error.e3} // NOTE THAT YOU CAN SEND AS MANY ERROR MESSAGES AS YOU LIKE TO A SINGLE ERROR BLOCK. THERE'S NO NEED TO MAKE // ERROR BLOCKS FOR EACH CONTROL! $obj->setRule(array("mandatory"=>array("e3", "Please specify your age"))); // add a label control to the form, having the name and id attributes set to "label_option", // being attached to the control having the name and id attributes set to "option", and // having as caption the text "Select an option" // in the template file, you'll be able to call this control's output by using {controls.label_option} $form->add("label", "label_option", "option", "Select an option"); // add a select control, having the name attribute set to "option[]" and the id attributes set to "option", // no default value, and some HTML attributes set like "multiple", "size" and "style" // notice the '&' - this is there in order for $obj to really point to the newly addded control in PHP4 // also note the '[]' at the end of the name in order to be able to submit selected options as an array // also note that the id attribute will be name of the control with the '[]' stripped and therefore // in the template file, you'll be able to call this control's output by using {controls.option} $obj = & $form->add("select", "option[]", "", array("multiple"=>"multiple", "size"=>3, "style"=>"height:60px")); // add some options to the select control $obj->addOptions(array("I like this PHP class", "I don't like this PHP class", "What's PHP?")); // make this control mandatory and, in case of error, send the "Please select an option" message to "e4" error block // in the template file, you'll be able to call the error messages sent to this error block by using {error.e4} // NOTE THAT YOU CAN SEND AS MANY ERROR MESSAGES AS YOU LIKE TO A SINGLE ERROR BLOCK. THERE'S NO NEED TO MAKE // ERROR BLOCKS FOR EACH CONTROL! $obj->setRule(array("mandatory"=>array("e4", "Please select an option"))); // add a label control to the form, having the name and id attributes set to "label_date", // being attached to the control having the name and id attributes set to "date", and // having as caption the text "What date is today?" // in the template file, you'll be able to call this control's output by using {controls.label_date} $form->add("label", "label_date", "date", "What date is today?"); // add a date control, having the name and id attributes set to "date" // notice the '&' - this is there in order for $obj to really point to the newly addded control in PHP4 // in the template file, you'll be able to call this control's output by using {controls.date} $obj = & $form->add("date", "date"); // set some properties of the date picker $obj->datePicker->preselectedDate = mktime(0, 0, 0, date("m"), date("d"), date("Y")); $obj->datePicker->dateFormat = "d M Y"; // make this control mandatory and, in case of error, send the "Please select a date" message to "e5" error block // in the template file, you'll be able to call the error messages sent to this error block by using {error.e5} // NOTE THAT YOU CAN SEND AS MANY ERROR MESSAGES AS YOU LIKE TO A SINGLE ERROR BLOCK. THERE'S NO NEED TO MAKE // ERROR BLOCKS FOR EACH CONTROL! $obj->setRule(array("mandatory"=>array("e5", "Please select a date"))); // place a submit button $form->add("submit", "submit", "Submit"); $form->add("file", "file"); // validate the form if ($form->validate()) { // code if form is valid print_r("Form is valid. Redirect or whatever..."); } // display the form with the specified template $form->render("example.xtpl"); ?> </body> </html>