Common Notices
• Use standard opening and closing tags <html>...</html>. Enclose everything else in these otherwise your page may display incorrectly.
• Do not put ApPHP DataValidator code into another HTML Form: <form>...</form>
Getting Started
Step 1. Creating Validator object.
Make sure you define a valid relative (virtual) path to the
Validator.class.php file.
define('VALIDATOR_DIR', '');
require_once(VALIDATOR_DIR.'validator.class.php');
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.
$validator = new Validator();
Step 2. General Settings.
You can specify some options for your validator class: if you want validation to be stopped after the first error comes up call
$validator->SetStopIfErrorFlag(1); otherwise leave it as a default. By default validator collects all
errors and stops only when there are no more data to validate.
$validator->SetStopIfErrorFlag(1);
Step 3. Creating ValidatorDataContainer object.
Now you need to set your validating data. By default $_REQUEST array is used, so if you want to
use a
$_REQUESTarray you can skip this part.
If you want to use an array
$someDataArray => array($key0 => $value0, $key1 => $value1); as a source
for your data, call method AddDataContainer:
$someDataArray => array($key0 => $value0, $key1 => $value1);
$validator->AddDataContainer($someDataArray);
Method AddDataContainer accepts any count of parameters - arrays or objects.
Step 4. Creating ValidationType objects.
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
ValidatorType such as
ValidatorTypeString or
ValidatorTypeUrl which matches the data type you want to validate.
Every Validation Type has additional options for a agile and accurate validation.
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]
it can be validated as follows:
$validatorType = new ValidatorTypeNumeric(
'age',
ValidatorTypeNumeric::$subtypeInt,
'user age'
);
$validatorType->SetMin(16);
$validatorType->SetMax(90);
$validator->AddType($validatorType);
$validator->Validate();
That's all! Validation is done.
Step 5. Error handling.
Once validation is finished you need to understand if there where any errors. Call a method:
$check = $validator->GetHasErrorStatus();
$check 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:
$errors = $validator->GetErrorArray();
$errors is an array of ValidatorError objects. By using them, you can either get a user friendly messages for every error:
foreach($errors as $error){
echo $error->ToString();
}
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.
$errorData = array();
foreach($errors as $error){
$errorData[$error->GetErrType()] = $error->GetErrData();
}
In this way you will receive all validation results.