PHP Classes

File: docs/GettingStarted.html

Recommend this page to a friend!
  Classes of Chara Miteo   ApPHP Data Validator   docs/GettingStarted.html   Download  
File: docs/GettingStarted.html
Role: Documentation
Content type: text/plain
Description: Getting Started
Class: ApPHP Data Validator
Validate sets of values in different ways
Author: By
Last change:
Date: 12 years ago
Size: 9,463 bytes
 

Contents

Class file image Download
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>ApPHP DataValidator :: Getting Started</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <style type="text/css"> .req { color:#960096; } .method { color:#009600; } .comments { color:#7a7a7a; } .comments_b { color:#575757; } .code { color:#000096; } BODY { padding:5px 10px; } PRE { background-color:#f1f2f3; border:1px solid #cccccc; padding:3px 5px; } </style> </head> <body> <!-- last update 13.04.2012 --> <div style="float:right;"> <div id="content-0"> <div class="post-0"> <h2 id="post-1">Getting Started of ApPHP DataValidator (version 1.0.0 or above)</h2> <div class="storycontent"> <table> <tbody> <tr> <td> <ul> <li><a href="GettingStarted.html#Common_Notices"><span>1.</span> <span>Common Notices</span></a><br /></li> <li><a href="GettingStarted.html#Getting_Started"><span>2.</span> <span>Getting Started</span></a><br /><br /> <ul> <li>&nbsp;&nbsp;&nbsp;&nbsp;<a href="GettingStarted.html#Step_1._Creating_Validator_Object."><span>Step 1. Creating Validator object.</span></a> </li> <li>&nbsp;&nbsp;&nbsp;&nbsp;<a href="GettingStarted.html#Step_2._General_Settings."><span>Step 2. General Settings.</span></a> </li> <li>&nbsp;&nbsp;&nbsp;&nbsp;<a href="GettingStarted.html#Step_3._Creating_ValidatorDataContainer_Object."><span>Step 3. Creating ValidatorDataContainer object.</span></a> </li> <li>&nbsp;&nbsp;&nbsp;&nbsp;<a href="GettingStarted.html#Step_4._Creating_ValidationType_Objects."><span>Step 4. Creating ValidationType objects.</span></a> </li> <li>&nbsp;&nbsp;&nbsp;&nbsp;<a href="GettingStarted.html#Step_5._Error_handling."><span>Step 5. Error handling.</span></a> </li> </ul> <br /> </li> </ul> </td> </tr> </tbody> </table> <a name="Common_Notices"></a> <h3>Common Notices</h3> <p> <font color="darkred">&#8226; <b>Use standard opening and closing tags &lt;html&gt;...&lt;/html&gt;. Enclose everything else in these otherwise your page may display incorrectly. </b></font><br /> <font color="darkred">&#8226; <b>Do not put ApPHP DataValidator code into another HTML Form: &lt;form&gt;...&lt;/form&gt; </b></font> </p> <a name="Getting_Started"></a> <h3><b>Getting Started</b></h3> <div style="FLOAT: right; MARGIN-LEFT: 5px">[<a title="Step 1. Creating Validator object." href="GettingStarted.html#top">top</a>]</div> <a name="Step_1._Creating_Validator_Object."></a> <h4>Step 1. Creating Validator object.<br /> ---------------------------------------------------------------------------</h4> <p> Make sure you define a valid relative (virtual) path to the <b>Validator.class.php</b> file. </p> <pre> <span class="comments">## *** include Validator class </span><span class="code">define('VALIDATOR_DIR', ''); require_once(VALIDATOR_DIR.'validator.class.php');</span> </pre> <p> There are two basic validator classes: Validator and ValidatorDataContainer. Class Validator manages a validation process. Class ValidatorDataContainer keeps the data you want to validate. First you have to create an object of Validator class. </p> <pre> <span class="comments">## *** create Validator object</span> <span class="code">$validator = new Validator();</span> </pre> <p><br/></p> <div style="FLOAT: right; MARGIN-LEFT: 5px">[<a title="Go to top" href="GettingStarted.html#top">top</a>]</div> <a name="Step_2._General_Settings."></a> <h4>Step 2. General Settings.<br /> ---------------------------------------------------------------------------</h4> <p> You can specify some options for your validator class: if you want validation to be stopped after the first error comes up call <i><b>$validator->SetStopIfErrorFlag(1);</b></i> otherwise leave it as a default. By default validator collects all errors and stops only when there are no more data to validate. </p> <pre> <span class="code">$validator->SetStopIfErrorFlag(1);</span> </pre> <p><br/></p> <div style="FLOAT: right; MARGIN-LEFT: 5px">[<a title="Step 1. Creating ValidatorDataContainer object." href="GettingStarted.html#top">top</a>]</div> <a name="Step_3._Creating_ValidatorDataContainer_Object."></a> <h4>Step 3. Creating ValidatorDataContainer object.<br /> ---------------------------------------------------------------------------</h4> <p> Now you need to create an object of ValidatorDataContainer class. If you want to use a <i><b>$_REQUEST</b></i> array as source for your data (data from a form), you don't need to pass any parameters to ValidatorDataContainer constructor: </p> <pre> <span class="code">$dataContainer = new ValidatorDataContainer();</span> </pre> <p> If you want to use an array <i><b>$someDataArray => array($key0 => $value0, $key1 => $value1);</b></i> as a source for your data, pass it as a parameter: <i><b>$dataContainer = new ValidatorDataContainer($someDataArray);</b></i> Now you have a container with a data and a validator where you put your data. </p> <pre> <span class="code">$someDataArray => array($key0 => $value0, $key1 => $value1);</span> <span class="code">$dataContainer = new ValidatorDataContainer($someDataArray);</span> </pre> <p><br/></p> <div style="FLOAT: right; MARGIN-LEFT: 5px">[<a title="Step 1. Creating ValidatorDataContainer object." href="GettingStarted.html#top">top</a>]</div> <a name="Step_4._Creating_ValidationType_Objects."></a> <h4>Step 4. Creating ValidationType objects.<br /> ---------------------------------------------------------------------------</h4> <p> You are ready now to create ValidationType objects - the entities which contains data about each validating value. These values could be: a number, a string, an email and a URL. Consequently you create an object of a class inheriting from <i><b>ValidatorType</b></i> such as <i><b>ValidatorTypeString</b></i> or <i><b>ValidatorTypeUrl</b></i> which matches the data type you want to validate. Every Validation Type has additional options for a agile and accurate validation. <br /><br /> To set these options, simply call a setter method for those you want to use. For example, if there is a key-value pair ["age" => 12] in <i><b>$dataContainer</b></i> it can be validated as follows: </p> <pre> <span class="comments">/* constructor gets for parameters: 1. a container object to look for data 2. a key matching the value you validate 3. a subtype (one of possible, they are public static properties whos names are started from "subtype") 4. optional friendly user name ('user age'), which is used to generate an error message */</span> <span class="code">$validatorType = new ValidatorTypeNumeric( $dataContainer, 'age', ValidatorTypeNumeric::$subtypeInt, 'user age' );</span> <span class="comments">// set a validation for minimum value</span> <span class="code">$validatorType->SetMin(16);</span> <span class="comments">// set a validation for maximum value</span> <span class="code">$validatorType->SetMax(90);</span> <span class="comments">// now the ValidatorType is ready and is added</span> <span class="code">$validator->AddType($validatorType);</span> <span class="comments">// after you add all values you want you can start the validation</span> <span class="code">$validator->Validate();</span> </pre> <p> That's all! Validation is done. </p> <p><br/></p> <div style="FLOAT: right; MARGIN-LEFT: 5px">[<a title="Step 1. Creating ValidatorDataContainer object." href="GettingStarted.html#top">top</a>]</div> <a name="Step_5._Error_handling."></a> <h4>Step 5. Error handling.<br /> ---------------------------------------------------------------------------</h4> <p> Once validation is finished you need to understand if there where any errors. Call a method: </p> <pre> <span class="code">$check = $validator->GetHasErrorStatus();</span> </pre> <p> <b><i>$check</i></b> is a boolean value that contains a 'true' if there where any errors, or a 'false' if there where none. If any errors have occurred, you can get them using another method: </p> <pre> <span class="code">$errors = $validator->GetErrorArray();</span> </pre> <p> <b><i>$errors</i></b> is an array of ValidatorError objects. By using them, you can either get a user friendly messages for every error: </p> <pre><span class="code">foreach($errors as $error){ echo $error->ToString(); } </span></pre> <p> Or use following another way to get the error type and additional data (container array key, minimum value etc.) to do whatever you want it for. </p> <pre><span class="code">$errorData = array(); foreach($errors as $error){ $errorData[$error->GetErrType()] = $error->GetErrData(); } </span></pre> In this way you will receive all validation results. <p><br/></p> </div> </div> </div> </body> </html>