Login   Register  
PHP Classes
elePHPant
Icontem

File: example.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Andrzej Pomian  >  Form Validator  >  example.php  >  Download  
File: example.php
Role: Example script
Content type: text/plain
Description: Example of usage
Class: Form Validator
Validate values submitted via Web forms
Author: By
Last change:
Date: 2004-04-18 12:20
Size: 7,755 bytes
 

Contents

Class file image Download
<?PHP
/**
 *
 * FormValidator
 *
 * Example of usage
 */

/*
  Parameters of form elem that are being processed by the class
$elem = array (
   'name'      => string,  // all
   'type'      => string,  // text or select
                           // text covers html types: text, textarea, hidden, password 
                           // select covers html types: select, checkbox and radio
   'label'     => string,  // field label ( eg. 'Phone number' )
   'required'  => boolean, // field must have value
   'cont'      => string,  // content type: email, word, alpha, digit
   'ereg'      => string,  // text, textarea
   'preg'      => string,  // text, textarea
   'len'       => integer, // accurate length
   'len_min'   => integer, // min length
   'len_max'   => integer, // max length
   'val_max'   => integer, // max value of an integer ( use with cont == digit )
   'val_min'   => integer, // min value of an integer ( use with cont == digit )
    'values'    => array;   // select accepted values
    'forbid'    => array;   // forbidden values that match other criteria
   'arr_size_min' => integer // when field name is an array( eg. 'phones[]' ) 
                           // minimum number of elements in array
   // Fields dependencies

   
   'eqal'      => mixed,   // array or string, 
                           // value of this field must be equal to value of field in array() 
                                   // eg. in when there are two password boxes either array or string
   'with'      => mixed,   // array or string, value must exist with other value(s)
   'alt_or'    => mixed,   // array or string, at least one of fields must have a value
   'alt_xor'   => mixed   // array or string, only one field must have a value
);
*/


require('FormValidator.class.php');

// Form elements definition
$elems[] = array('name'=>'sex','label'=>'sex''type'=>'select''required'=>true'values' => array('Mr.''Mrs.''Miss''Ms.'));
$elems[] = array('name'=>'fname','label'=>'Firstname''type'=>'text''required'=>true'len_max'=>'30');
$elems[] = array('name'=>'sname','label'=>'Family Name''type'=>'text''required'=>true'len_max'=>'30');
$elems[] = array('name'=>'strsel''type'=>'select''required'=>true'values' => array('street','square'));
$elems[] = array('name'=>'street''type'=>'text''required'=>true'len_max'=>'30');
$elems[] = array('name'=>'strno''type'=>'text''required'=>true'len_max'=>'5');
$elems[] = array('name'=>'appartment''type'=>'text''len_max'=>'4');
$elems[] = array('name'=>'pcode1''type'=>'text''required'=>true'len'=>'2''cont' => 'digit');
$elems[] = array('name'=>'pcode2''type'=>'text''required'=>true'len'=>'3''cont' => 'digit');
$elems[] = array('name'=>'city''type'=>'text''required'=>true'len_max'=>'30');
$elems[] = array('name'=>'phone''type'=>'text''required'=>true'len_max'=>'30');
$elems[] = array('name'=>'email''type'=>'text''required'=>true'len_max'=>'30''cont' => 'email');
$elems[] = array('name'=>'pass1''type'=>'text''required'=>true'len_min'=>'5''len_max'=>'30');
$elems[] = array('name'=>'pass2''type'=>'text''required'=>true'len_min'=>'5''len_max'=>'30''equal'=> array('pass1'));
$elems[] = array('name'=>'ppref''type'=>'text''len'=>'2''arr_size_min'=>1'with'=>'pnum''cont' => 'digit');
$elems[] = array('name'=>'pnum''type'=>'text''len'=>'7''arr_size_min'=>1'with'=>'ppref''cont' => 'digit');


// validate form
if ( $_GET['a'] == 's' ) {
    
$f = new FormValidator($elems);
    
$err $f->validate($_GET);
    
    if ( 
$err === true ) {
        echo 
'<br>You need to correct following fields<br><br>';
        
        
$valid $f->getValidElems();
        
        foreach ( 
$valid as $k => $v ) {
            if ( 
$v[1] == false ) {
                
// Empty label field
                
if ( empty($v[2]) ) {
                    
// then echo the form name of a field
                    
echo $k.'<br>';
                }
                else {
                    echo 
$v[2].'<br>';
                }
            }
        }
    }
    else {
        echo 
'<br><h3>Form was successfully validated with no errors!</h3><br><br>';
    }
    
}

?>

<form action="example.php" method="get">
<input type="hidden" name="a" value="s">
<table>
<tr>
    <td>&nbsp;</td>
    <td>
        <select name="sex">
            <option <?php if ($_GET['sex']=="Mr.") { echo 'selected';} ?>>Mr.</option>
            <option <?php if ($_GET['sex']=="Mrs.") { echo 'selected';} ?>>Mrs.</option>
            <option <?php if ($_GET['sex']=="Miss") { echo 'selected';} ?>>Miss</option>
            <option <?php if ($_GET['sex']=="Ms.") { echo 'selected';} ?>>Ms.</option>
        </select>
    </td>
</tr>
<tr>
    <td>Name(s)</td>
    <td>
        <input name="fname" size="18" value="<?= $_GET['fname'?>" class="{$err_c.fname[0]}" />
    </td>
</tr>
<tr>
    <td>Family name</td>
    <td>
        <input name="sname" size="18" value="<?= $_GET['sname'?>" class="{$err_c.sname[0]}" />
    </td>
</tr>
<tr>
    <td> Address of residence</td>
    <td>
        <table>
        <tr>
            <td colspan="2"> &nbsp;street</td>
            <td> No</td>
            <td> appartment</td>
        </tr>
        <tr>
            <td>
                <select name="strsel">
                    <option <?php if ($_GET['strsel']=='street') { echo 'selected';} ?>>street</option>
                    <option <?php if ($_GET['strsel']=='square') { echo 'selected';} ?>>square</option>
                </select>
            </td>
            <td>
                <input size="10" name="street" value="<?= $_GET['street'?>"" class="{$err_c.street[0]}" />
            </td>
            <td>
                <input size="2" name="strno" value="<?= $_GET['strno'?>" class="{$err_c.strno[0]}" />
            </td>
            <td>
                <input size="2" name="appartment" value="<?= $_GET['appartment'?>" class="{$err_c.appartment[0]}" />
            </td>
        </tr>
        </table>
    </td>
</tr>
<tr>
    <td> Post Code</td>
    <td>
        <input maxlength="2" size="2" name="pcode1" value="<?= $_GET['pcode1'?>" class="{$err_c.pcode1[0]}" />-
        <input maxlength="3" size="3" name="pcode2" value="<?= $_GET['pcode2'?>" class="{$err_c.pcode2[0]}" />
    </td>
</tr>
<tr>
    <td> City</td>
    <td>
        <input name="city" size="18" value="<?= $_GET['city'?>" class="{$err_c.city[0]}" />
    </td>
</tr>
<tr>
    <td> Contact Phone</td>
    <td>
        <input name="phone" size="18" value="<?= $_GET['phone'?>" class="{$err_c.phone[0]}" />
    </td>
</tr>
<tr>
    <td> E-mail</td>
    <td>
        <input name="email" size="18" value="<?= $_GET['email'?>" class="{$err_c.email[0]}" />
    </td>
</tr>
<tr>
    <td> Hasło minimum 5 letters</td>
    <td>
        <input type="password" value="<?= $_GET['pass1'?>" name="pass1" size="18" class="{$err_c.pass1[0]}" />
    </td>
</tr>
<tr>
    <td> Confirm password</td>
    <td>
        <input type="password" value="<?= $_GET['pass2'?>" name="pass2" size="18" class="{$err_c.pass2[0]}" />
    </td>
</tr>
<tr>
    <td> Phone numbers you are calling from phone number</td>
    <td >
        <table>
        <tr>
            <td> city code</td>
            <td> phone numbers</td>
            <td>&nbsp;</td>
        </tr>

        <tr>
            <td>
                <input maxlength="2" size="2" name="ppref[]" value="<?= $_GET['ppref'][0?>" class='{$err_c.ppref[0]}' />
            </td>
            <td>
                <input maxlength="7" size="8" name="pnum[]" value="<?= $_GET['pnum'][0?>" class='{$err_c.pnum[0]}' />
            </td>
            <td>&nbsp;</td>
        </tr>
        
        <tr>
            <td>
                <input maxlength="2" size="2" name="ppref[]" value="<?= $_GET['ppref'][1?>" class='{$err_c.ppref[1]}' />
            </td>
            <td>
                <input maxlength="7" size="8" name="pnum[]" value="<?= $_GET['pnum'][1?>" class='{$err_c.pnum[1]}' />
            </td>
            <td>&nbsp;</td>
        </tr>

        <tr>
            <td>
                <input maxlength="2" size="2" name="ppref[]" value="<?= $_GET['ppref'][2?>" class='{$err_c.ppref[2]}' />
            </td>
            <td>
                <input maxlength="7" size="8" name="pnum[]" value="<?= $_GET['pnum'][2?>" class='{$err_c.pnum[2]}' />
            </td>
            <td>&nbsp;</td>
        </tr>
        
        <tr>
            <td>&nbsp;</td>
            <td>
                <div align="right"> <br />
                    <input type="submit" value="send" />
                </div>
            </td>
            <td>&nbsp;</td>
        </tr>
        </table>
    </td>
</tr>
</table>

</body>
</html>