Location: PHPKode > scripts > Naf_Validator > naf_validator/example/index.php

 * Naf_Validator example script
 * @author Victor Bolshov <hide@address.com>

	// 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);

<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" />

Return current item: Naf_Validator