PHP Classes

File: example/index.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in Bookmark in
  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: 8 years ago
Size: 1,733 bytes


Class file image Download

 * Naf_Validator example script
 * @author Victor Bolshov <>

// we are about to validate $_POST array
    // include libraries
require_once dirname(__FILE__) . "/../Validator.php";
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);

<title>Naf_Validator example script</title>
<?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>
<?php endif; ?>

<h3>Example comment form</h3>

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

    <input type="submit" />