PHP Classes

File: example4.php

Recommend this page to a friend!
  Classes of M H Rasel   Form Validation   example4.php   Download  
File: example4.php
Role: Example script
Content type: text/plain
Description: Depends
Class: Form Validation
Validate submitted form values according to rules
Author: By
Last change:
Date: 14 years ago
Size: 3,036 bytes
 

Contents

Class file image Download
<?php

 
/**
  * -------------------------------------------------------------------------
  * PHP Form Validator (formvalidator.php)
  * Version 1.0
  * -------------------------------------------------------------------------
  * @author M H Rasel
  * 4-5-2009::0:15
  * @copyright 2009
  * email:mail@monjur-ul-hasan.info
  * Documentation: http://phpformvalidation.monjur-ul-hasan.info
 */

 
  
require "class.validator.inc";
  
// if form is posted
  
if($_POST['action']=="Check")
   {
       
      
$data = $_POST;
   
       
$rules = array(
       
"Yes"=> array(
           
"type" => "checkbox" // declering the form field as checkbox
       
),
       
   
       
"email"=>array(
               
"depend"=>array(
                   
"depend_on" =>"Yes", // set the depend_form field , this rule
                                            // will be checked only when the Yes form field is set.
                                            // other then this entire rule will be escaped
                   
"require" =>true,
                   
"email" => true,
            )
        )
        );
       
       
// set the error message
       
$message = array(
               
"email"=>array(
                           
"depend"=>array(
                               
"email"=>"Invalid email address"
                           
)
                )
        );
       
       
$objValidator = new Validator($rules,$message);
           
echo
"<h2><a href='#'>Validator Result </a></h2>";
// call the validation proce
if(!$objValidator->isValid($data))
{
   
// error found
   
    // get the list of form fields which have errorful data
   
$errors = $objValidator->ErrorFields();
   
// show the number of error
   
echo "<br/> you have total ".$objValidator->CountError()." errors.<br/>Fields are :".implode(",",$errors);
    echo
"<br/><hr/>";
   
    foreach(
$errors as $key=>$var)
    {
        echo
"For the field <b>$var</b> errors are: ";
       
// collect error for each error full field
       
$value = $objValidator->getErrors($var);
       
        if(
is_array($value))
           
// if the field have more then one validation rule faild then an array will return
           
echo implode(" * ",$value);
        else echo
$value;
        echo
"<br/>";
    }
        }
        else echo
"valid";
   }
?>



<form method="POST" action="">
    <table border="1" width="100%">
        <tr>
            <td>Click the check box to get newsletter in future
            <input type="checkbox" name="Yes" value="OK" <?php echo ($data["Yes"]=="OK"?"Checked":"") ?>></td>
        </tr>
        <tr>
            <td>Email Address:&nbsp; <input type="text" name="email" value="<?php echo $data["email"]?>" size="20"></td>
        </tr>
        </table>
    <p><input type="submit" value="Check" name="action"><input type="reset" value="Reset" name="B2"></p>
</form>