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

//contains posted values
$value = array();
//contains got errors
$errors = array();
//contains messages
$msg = '';

//if submit button is pressed
if(isset($_REQUEST['submit'])){
    $value = $_REQUEST;
    //create validator
    $validator = new Validator();
    //EMAIL
    //create Email validation type
    $emailValidator = new ValidatorTypeEmail('email', 'user email');
    // by default a field can be null, but now email is necessary
    $emailValidator->SetCanBeNullFlag(0);
    //put email into validator
    $validator->AddType($emailValidator);

    //AGE
    // create Age validation type
    $ageValidator = new ValidatorTypeNumeric('age', ValidatorTypeNumeric::$subtypeNumeric);
    // by default a field can be null, but now age is necessary
    $ageValidator->SetCanBeNullFlag(0);
    // age can not be less then 16
    $ageValidator->SetMin(16);
    // age can not be greater then 130
    $ageValidator->SetMax(130);
    // add age to validator
    $validator->AddType($ageValidator);

    //start validation
    $validator->Validate();

    //get errors
    if ($validator->GetHasErrorStatus() ) {
        // loop through error array
        foreach($validator->GetErrorArray() as $oneError){
            //collect all found errors in an array for every field
            $errors[$oneError->GetFieldName()][] = $oneError->ToString();
        }
    }
    if(empty($errors) ){
        $msg = '<span style="color:green">Your data has been successfully validated!</span>';
    }
}?>

<!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 (simple)</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; }
        .caption {width: 120px; float: left; }
        .error { font-size:12px; color: red; }
        .warning { font-size:12px; color: gray; }
        .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 echo ((!empty($msg)) ? '<div class="message">'.$msg.'</div>' : '<br><br>'); ?>

    <fieldset>
    <legend>Form (simple)</legend>
    <form action="" method="post">
    <div class='formDiv'>
        <div class="caption">User email:</div>
        <input type='text' value='<?php echo isset($value['email'] ) ? $value['email'] : '' ?>' name='email'>    
        <div class="error"><?php echo isset($errors['email']) ? implode('<br>', $errors['email'] ): ''?></div>
    </div>
    <div class='formDiv'>
        <div class="caption">Age:</div>
        <input type="text" value='<?php echo isset($value['age']) ? $value['age'] : ''?>' name='age'>    
        <div class="warning">*numbers only, from 16 to 130</div>
        <div class="error"><?php echo isset($errors['age']) ? implode('<br>', $errors['age'] ): ''?></div>
    </div>
    <div><br /><input type='submit' name='submit' value='Submit'></div>
    </form>
    </fieldset>
</body>
</html>
Return current item: ApPHP DataValidator Basic