PHP Classes

File: example.php

Recommend this page to a friend!
  Classes of t withers   Model Validation   example.php   Download  
File: example.php
Role: Example script
Content type: text/plain
Description: Example using Validation Class
Class: Model Validation
Validate form values according to many rules
Author: By
Last change:
Date: 11 years ago
Size: 1,932 bytes
 

Contents

Class file image Download
<?php
$validate
=array(
   
'text'=>array(
       
'label'=>'Your text',
       
'required'=>true,
       
'minlength'=>5,
       
'maxlength'=>10,
    ),
   
'contact[name]'=>array(
       
'label'=>'Name',
       
'required'=>true,
       
'characters'=>'alpha',
       
'minlength'=>2,
       
'maxlength'=>20,
    ),
   
'contact[email]'=>array(
       
'label'=>'Email',
       
'characters'=>'email',
    ),
   
'contact[password]'=>array(
       
'label'=>'The password',
       
'characters'=>'alphanum',
       
'minlength'=>6,
       
'maxlength'=>32,
       
'verify'=>'repeat',
    ),
   
'anythingbut'=>array(
       
'label'=>'Anything',
       
'not'=>1,
    )
   
);
include(
'Validation.php');
$valid=true;
if(
$_SERVER['REQUEST_METHOD']=='POST')
   
$valid=Validation::set($validate,true);//setting to true will break after a single error. false will put all errors into an array.
?>



<!-- form method can be post or get, php uses the $_REQUEST variable to access the data -->
<?php if(!$valid){
   echo
implode(" ",Validation::getErrors ());
}else if(
$valid && $_SERVER['REQUEST_METHOD']=='POST'){
    echo
'Looks good!<br/>';
}
?>

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
Text:<textarea name="text"><?php echo isset($_POST['text'])?$_POST['text']:'';?></textarea><br/>
Name:<input type="text" name="contact[name]" value="<?php echo isset($_POST['contact']['name'])?$_POST['contact']['name']:'';?>"/><br/>
Email:<input type="text" name="contact[email]" value="<?php echo isset($_POST['contact']['email'])?$_POST['contact']['email']:'';?>" /><br/>
Password:<input type="password" name="contact[password]" /><br/>
Repeat:<input type="password" name="repeat" /><br/>
Anything but 1:<input type="text" name="anythingbut" value="<?php echo isset($_POST['anythingbut'])?$_POST['anythingbut']:1;?>"/><br/>
<input type="submit"/>
</form>