PHP Classes

File: examples/example_form.php

Recommend this page to a friend!
  Classes of Chara Miteo   ApPHP Data Validator   examples/example_form.php   Download  
File: examples/example_form.php
Role: Example script
Content type: text/plain
Description: Example file
Class: ApPHP Data Validator
Validate sets of values in different ways
Author: By
Last change:
Date: 11 years ago
Size: 7,095 bytes
 

Contents

Class file image Download
<?php
// class Validator
require_once '../validator.class.php';

class
ValidatorExample
{
    static private
$errorArray;

    static private
$result;

    static private
$data;


    static public function
Execute() {
       
self::$data = new ValidatorDataContainer();

        if(!
self::$data->IfFieldExists('submit')){
            return;
        }
       
$validator = new Validator();
       
//$validator->SetStopIfErrorFlag(0);

        //first name
       
$validFirstName = new ValidatorTypeString(self::$data, 'first_name', ValidatorTypeString::$subtypeAlphabetic, 'first name');
       
$validFirstName->SetMinLen(3);
       
$validFirstName->SetMaxLen(30);
       
$validator->AddType($validFirstName);

       
//second name
       
$validSecondName = new ValidatorTypeString(self::$data, 'last_name', ValidatorTypeString::$subtypeAlphabetic, 'last name');
       
$validSecondName->SetMinLen(3);
       
$validSecondName->SetMaxLen(30);
       
$validator->AddType($validSecondName);

       
//age
       
$validAge = new ValidatorTypeNumeric(self::$data, 'age', ValidatorTypeNumeric::$subtypeNumeric, 'age');
       
$validAge->SetMin(1);
       
$validAge->SetMax(130);
       
$validator->AddType($validAge);

       
//email
       
$validEmail = new ValidatorTypeEmail(self::$data, 'email', 'email');
       
$validator->AddType($validEmail);

       
//site
       
$validSite = new ValidatorTypeUrl(self::$data, 'site', 'site');
       
$validSite->SetCanBeNullFlag(1);
       
$validator->AddType($validSite);

       
//country
       
$validCountry = new ValidatorTypeString(self::$data, 'country', ValidatorTypeString::$subtypeAlphabetic, 'country of origin');
       
$validCountry->SetMinLen(3);
       
$validCountry->SetMaxLen(100);
       
$validCountry->SetCanBeNullFlag(0);
       
$validCountry->SetSpacesAllowedFlag(1);
       
$validator->AddType($validCountry);

       
$validator->Validate();

        if (
$validator->GetHasErrorStatus()) {
           
$errorArray = $validator->GetErrorArray();
        }

        if (isset(
$errorArray)) {
           
self::$result = '<span style="color:red">Your data is invalid!</span>';
            foreach (
$errorArray as $error) {
               
self::$errorArray[$error->GetFieldName()][] = $error->ToString();
            }

        } else {
           
self::$result = '<span style="color:green">Your data has been successfully validated!</span>';
        }
    }

    static public function
GetErrorArray() {
        return
self::$errorArray;
    }

    static public function
GetResult() {
        return
self::$result;
    }

    static public function
GetErrorForField($fieldName) {
        if (isset(
self::$errorArray[$fieldName])) {
           
$errorLine = '';
            foreach (
self::$errorArray[$fieldName] as $error) {
               
$errorLine .= $error . '<br>';
            }
            return
$errorLine;
        }
    }

    static public function
GetFieldValue($name){
        if(
self::$data->IfFieldExists($name)){
            return
self::$data->GetValue($name);
        }
        return
'';
    }

    static public function
Layout(){
       
$msg = self::GetResult();
        return
'
            '
.((!empty($msg)) ? '<div class="message">'.$msg.'</div>' : '<br /><br />').'
            <fieldset>
            <legend>Form (advanced)</legend>
            <form name="myForm" method="post" action="">
                <div class="formDiv">
                    <div class="caption">First name:</div>
                    <input type="text" value="'
.self::GetFieldValue('first_name').'" name="first_name">
                    <div class="comments">*one world, letters only</div>
                    <div class="error">'
.self::GetErrorForField('first_name').'</div>
                </div>
                <div class="formDiv">
                    <div class="caption">Last name:</div>
                    <input type="text" value="'
.self::GetFieldValue('last_name').'" name="last_name">
                    <div class="comments">*one world, letters only</div>
                    <div class="error">'
.self::GetErrorForField('last_name').'</div>
                </div>
                <div class="formDiv">
                    <div class="caption">Age:</div>
                    <input type="text" value="'
.self::GetFieldValue('age').'" name="age">
                    <div class="comments">*numbers only, max 130</div>
                    <div class="error">'
.self::GetErrorForField('age').'</div>
                </div>
                <div class="formDiv">
                    <div class="caption">Email:</div>
                    <input type="text" value="'
.self::GetFieldValue('email').'" name="email">
                    <div class="error">'
.self::GetErrorForField('email').'</div>
                </div>
                <div class="formDiv">
                    <div class="caption">Site (optional):</div>
                    <input type="text" value="'
.self::GetFieldValue('site').'" name="site">
                    <div class="error">'
.self::GetErrorForField('site').'</div>
                </div>
                <div class="formDiv">
                    <div class="caption">Country of origin:</div>
                    <input type="text" value="'
.self::GetFieldValue('country').'" name="country">
                    <div class="comments">*letters only</div>
                    <div class="error">'
.self::GetErrorForField('country').'</div>
                </div>
                <div><input type="submit" name="submit" value="Submit"></div>
            </form>
            </fieldset>'
;
    }
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
  <head>
    <title>ApPHP DataValidator :: Examples :: Web Form (advanced)</title>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
    <meta name='keywords' content='php data validator, php datavalidator, php validate data, form validation' />
    <meta name='description' content='Advanced Power of PHP - using of ApPHP DataValidator' />
    <meta content='Advanced Power of PHP' name='author'></meta>
    <style type="text/css">
        BODY { padding:15px; }
        FIELDSET { width:450px; border:1px solid #dfdfdf; margin-bottom:10px; }
        LEGEND {margin-bottom:3px; font-weight:bold; }
        .error { font-size: 12px; color: red; }
        .caption {width: 150px; float: left; }
        .formDiv {padding-bottom: 10px; }
        .comments{ font-size: 12px; display: inline; }
        .message { padding:5px; margin:10px 0px 10px 2px; width:463px; color: #000000; background-color:#fffff1; border:1px solid #c1c13a; }
    </style>
</head>
<body>
<a href="index.php">Back to Index Page</a>
<?php
// validates the form
ValidatorExample::Execute();
// shows the form
echo ValidatorExample::Layout();
?>
</body>
</html>