Location: PHPKode > scripts > ApPHP DataValidator Basic > examples/example_form.php
<?php
// class Validator
require_once '../validator.class.php';

class ValidatorExample
{
    static private $errorArray;

    static private $result;

    static public function Execute() {
        if(!isset($_REQUEST['submit'])){
            return;
        }
        $validator = new Validator();
        //$validator->SetStopIfErrorFlag(0);

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


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

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

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

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

        //country
        $validCountry = new ValidatorTypeString('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(isset($_REQUEST[$name])){
            return $_REQUEST[$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'/>
    <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>
Return current item: ApPHP DataValidator Basic