Introduction to the PHP class "forms and validation"

Description

The creation of HTML-forms is a time consuming task. Also the creation of validation rules has to be done and is always quite similar and still has to be tested intensively. The PHP class "forms and validation" can reduce the necessary time to create input forms on your website to a minimum. In addition there are a lot of validation rules included to make your life easier.

I created some examples to show you how easy you can use this PHP class. You can try every example by your own to get an feeling for the power of the creation and validation capabilities and this class. Also you can directly see the used source code to boost your learning curve of this class functions. In the end there is a complete list of the public functions of this class.

I hope this class saves you a lot of time so you can focus on the real challenging programming tasks.

Index of this page:

Example 1: contact form

Please fill out the form and send us your message...

 
 
 
  

Source Code


$fav = new formsAndValidation('ex1');
$fav->setTarget('index.php');
$fav->addtitle('Example 1: contact form');

if(isset($_POST['ex1']) AND $_POST['ex1'] == 'send'){
  $fav->save2Session();
  $fav->isEmpty('name', 'please enter your name');
  $fav->isEmailAddress('email', 'please enter a valid eMail');
  $fav->isEmpty('message', 'please enter a message');
  if( $fav->getErrorCount() == 0 ){ 
    $fav->showSuccess('Thank you', 'Your message was send to us. ');
  }
}

$fav->addParagraph('Please fill out the form and send us your message...');
$fav->addTextInput('Your name', 'name');
$fav->addTextInput('Your eMail', 'email');
$fav->addTextArea('Your message', 'message');
$fav->showForm('Send request'); 

Example 2: order a pizza

Please choose the size of your pizza and the desired topings


Ham
Olives
extra cheese
hot like hell
medium
no spices
 
  

Source Code


$fav = new formsAndValidation('ex2');
$fav->setTarget('index.php');
$fav->addtitle('Example 2: order a pizza');

if(isset($_POST['ex2']) AND $_POST['ex2'] == 'send'){
  $fav->save2Session();
  $fav->isSelected('size', 'no size?');
  $fav->isSelected('spicy', 'spicy?');
  $fav->isEmpty('address', 'no address?');
  if( $fav->getErrorCount() == 0 ){ 
    $fav->showSuccess('Pizza is coming...');
    $fav->cleanSession();
  }
}

$fav->addParagraph('Please choose the size of your pizza and the desired topings');
$fav->addSelectList('size?', 'size', array('', '28', '30', '36') );
$fav->addCheckboxes('topings?', 'topings', array('Ham', 'Olives', 'extra cheese') );
$fav->addRadioButtons('spicy?', 'spicy', array('h' => 'hot like hell', 'm' => 'medium', 'n' => 'no spices') );
$fav->addTextArea('address?', 'address');
$fav->showForm('Send order'); 

Example 3: more predefined validations

 
test

the following input fields are mandatory!

 
male
female
 
  

Source Code


$fav = new formsAndValidation('ex3');
$fav->setTarget('index.php');
$fav->setShowErrorsOnTop(true);
$fav->setShowErrorsInForm(false);
$fav->loadDatepicker(true);
$fav->addtitle('Example 3: more predefined validations');

if(isset($_POST['ex3']) AND $_POST['ex3'] == 'send'){
  $fav->save2Session();
  $fav->isFutureDate('date', 'add a future date');
  $fav->isValidDate('date', 'use format mm/dd/YYYY');
  $fav->isEmpty('password', 'add a password with 8 or more letters', '8');
  $fav->isWithinRange('5dig', 'value is not a 5 digit number', 10000, 99999);
  $fav->isFile('upload', 'add a file');
  $fav->checkReload('please do not reload.');
  if( $fav->getErrorCount() == 0 ){ 
    $fav->showSuccess('Perfect', 'more than perfect...');
    $fav->cleanSession('ex3');
  }
}

$fav->addTextInput('Choose a date', 'date', '', array('id' => 'datepicker'));
$fav->addPassword('Passwort', 'password', '', '', 'test');
$fav->addHeadline('the following input fields are mandatory!');
$fav->addTextInput('5 digit number', '5dig', '');
$fav->addDefaultValue('gender', 'f');
$fav->addRadioButtons('Gender', 'gender', array('m' => 'male', 'f' => 'female'));	
$fav->addUpload('File', 'upload');
$fav->showForm('send'); 

Example 4: define own validations

use US format (###)###-####
 
 
  

Source Code


$fav = new formsAndValidation('ex4');
$fav->setTarget('index.php');
$fav->addtitle('Example 4: define own validations');

if(isset($_POST['ex4']) AND $_POST['ex4'] == 'send'){
  $fav->save2Session();
  $fav->checkWithRegExpression('regExp', 'use US phone format', '/\(?\d{3}\)?[-\s.]?\d{3}[-\s.]\d{4}/x');
  $fav->checkWithOwnValidation(array('field1', 'field2'), 'Number 1 is not equal to number 2', '$field1 == $field2');
  if( $fav->getErrorCount() == 0 ){ 
    $fav->showSuccess('Perfect', 'more than perfect...');
    $fav->cleanSession('ex4');
  }
}

$fav->addTextInput('Phone number', 'regExp', '', '', 'use US format (###)###-####');
$fav->addTextInput('Number 1', 'field1');
$fav->addTextInput('Number 2', 'field2');
$fav->showForm('send');


Fatal error: Uncaught Error: Call to undefined function mysql_connect() in /homepages/u9236/demo/formsAndValidation/index.php:305 Stack trace: #0 {main} thrown in /homepages/u9236/demo/formsAndValidation/index.php on line 305