Getting Started of ApPHP DataValidator (v1.0.0 or above)

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

[top]

Step 1. Creating Validator object.


Make sure you define a valid relative (virtual) path to the Validator.class.php file.
## *** include Validator class
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.
## *** create Validator object
$validator = new Validator();


[top]

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


[top]

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.


[top]

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:
/* constructor gets for parameters:
1. a key matching the value you validate
2. a subtype (one of possible, they are public static properties whose names are
   started from "subtype")
3. optional user-friendly name ('user age'), which is used to generate an error
   message
*/
$validatorType = new ValidatorTypeNumeric(
  'age',
  ValidatorTypeNumeric::$subtypeInt,
  'user age'
);
// set a validation for minimum value
$validatorType->SetMin(16);
// set a validation for maximum value
$validatorType->SetMax(90);
// now the ValidatorType is ready and is added
$validator->AddType($validatorType);
// after you add all values you want you can start the validation
$validator->Validate();
That's all! Validation is done.


[top]

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.