PHP Classes

File: example/index.php

Recommend this page to a friend!
  Classes of Victor Bolshov   Naf_Validator   example/index.php   Download  
File: example/index.php
Role: Example script
Content type: text/plain
Description: example of usage
Class: Naf_Validator
Validate user data with the filter extension
Author: By
Last change: changed priority
Date: 16 years ago
Size: 1,733 bytes
 

Contents

Class file image Download
<?php

/**
 * Naf_Validator example script
 *
 * @author Victor Bolshov <crocodile2u@yandex.ru>
 */

if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
   
// we are about to validate $_POST array
   
    // include libraries
   
require_once dirname(__FILE__) . "/../Validator.php";
    require_once
dirname(__FILE__) . "/../Validator/Result.php";
   
   
// create validator instance
   
$validator = new Naf_Validator();
   
   
// create rules.
    // note the chain-calls: Naf_Validator's add*() methods support "fluent interfaces",
    // so you do not need to write it that way:
    // $validator->addRequired(...);
    // $validator->addIntegerRule(...); etc
   
$validator->addRequired('author', "Author nickname is required")
        ->
addRequired('text', "Comment text is required")
        ->
addStringRule('author', "Author nickname must be a string")
        ->
addStringRule('text', "Comment text must be a string");
   
   
// now let's get the validation result.
    // note: Naf_Validator->check() returns an instance of Naf_Validator_Result!
   
$result = $validator->check($_POST);
   
}

?>
<html>
<head>
<title>Naf_Validator example script</title>
</head>
<body>
<?php if (isset($result)) : ?>
<ul style="border-bottom:1px solid black;">
        <li>Validation passed: <?php var_export($result->ok()); ?></li>
        <li>Collected errors: <?php var_export($result->getErrorList()); ?></li>
        <li>Filtered data: <?php var_export($result->export()); ?></li>
    </ul>
<?php endif; ?>

<h3>Example comment form</h3>

<form method="post" action="">
   
    <label>Nickname</label>
    <input type="text" name="text" />
    <br />
   
    <label>Comment</label>
    <input type="text" name="author" />
    <br />

    <input type="submit" />
   
</form>

</body>
</html>