Location: PHPKode > scripts > ApPHP DataValidator Basic > examples/example_array_script.php
<?php
require_once '../validator.class.php';
$validator = new Validator();

$dataArray = array(
    'number_normal' => 5,
    'number_less_then_min' => 1,
    'number_null_can_be_null' => 0,
    'number_float_when_int_needed' => 7.1,
    'number_null_can_not_be_null' => 0
);
$validator->AddDataContainer($dataArray);

//int 5 normal
$validatorType = new ValidatorTypeNumeric('number_normal', ValidatorTypeNumeric::$subtypeInt);
$validator->AddType($validatorType);

// int less then minimum
$validatorType = new ValidatorTypeNumeric('number_less_then_min', ValidatorTypeNumeric::$subtypeInt);
$validatorType->SetMin(16);
$validatorType->SetMax(90);
$validator->AddType($validatorType);

// int 0 and can be null with min
$validatorType = new ValidatorTypeNumeric('number_null_can_be_null', ValidatorTypeNumeric::$subtypeInt);
$validatorType->SetCanBeNullFlag(1);
$validatorType->SetMin(5);
$validator->AddType($validatorType);

//float when int is needed
$validatorType = new ValidatorTypeNumeric('number_float_when_int_needed', ValidatorTypeNumeric::$subtypeInt);
$validatorType->SetCanBeNullFlag(0);
$validatorType->SetMin(6);
$validator->AddType($validatorType);

//int 0 when can not be null
$validatorType = new ValidatorTypeNumeric('number_null_can_not_be_null', ValidatorTypeNumeric::$subtypeInt);
$validatorType->SetCanBeNullFlag(0);
$validator->AddType($validatorType);

//STRING
$dataArray = array(
    'string_noraml' => 'a',
    'string_smaller_then_min' => 'be',
    'string_alphabetic_with_digits' => 'Henry8',
    'string_no_spaces_with_pointing' => 'Jane:|',
    'string_with_pointing_on_no_pointing_setting' => 'Come2me!',

);
 $validator->AddDataContainer($dataArray);

// Normal string
$validatorType = new ValidatorTypeString( 'string_noraml', ValidatorTypeString::$subtypeAlphabetic);
$validatorType->SetCanBeNullFlag(0);
$validator->AddType($validatorType);

// string smaller then minLen
$validatorType = new ValidatorTypeString('string_smaller_then_min', ValidatorTypeString::$subtypeAlphabetic);
$validatorType->SetCanBeNullFlag(0);
$validatorType->SetMinLen(4);
$validator->AddType($validatorType);

// string alphabetic but with digits
$validatorType = new ValidatorTypeString('string_alphabetic_with_digits', ValidatorTypeString::$subtypeAlphabetic);
$validatorType->SetCanBeNullFlag(0);
$validatorType->SetMinLen(4);
$validatorType->SetMaxLen(12);
$validatorType->SetPointingAllowedFlag(0);
$validator->AddType($validatorType);

// string alphabetic without spaces and with pointing on no-spaces setting
$validatorType = new ValidatorTypeString('string_no_spaces_with_pointing', ValidatorTypeString::$subtypeAlphabetic);
$validatorType->SetCanBeNullFlag(0);
$validatorType->SetMinLen(4);
$validatorType->SetMaxLen(12);
$validatorType->SetSpacesAllowedFlag(0);
$validatorType->SetPointingAllowedFlag(1);
$validator->AddType($validatorType);

// string alphanumeric without spaces and with pointing on no spaces + no pointing setting
$validatorType = new ValidatorTypeString('string_with_pointing_on_no_pointing_setting', ValidatorTypeString::$subtypeAlphanumeric);
$validatorType->SetCanBeNullFlag(0);
$validatorType->SetMinLen(4);
$validatorType->SetMaxLen(20);
$validatorType->SetSpacesAllowedFlag(0);
$validatorType->SetPointingAllowedFlag(0);
$validator->AddType($validatorType);

//EMAIL
$data = array(
    'email_normal' => 'hide@address.com',
    'email_with_wrong_symbol' => 'my!hide@address.com',
);

$validator->AddDataContainer($data);

// normal email
$validatorType = new ValidatorTypeEmail('email_normal');
$validator->AddType($validatorType);

// wrong symbols
$validatorType = new ValidatorTypeEmail('email_with_wrong_symbol');
$validator->AddType($validatorType);

//URL
$data = array(
    'url_normal' => 'www.google.com',
    'url_with_properties' => 'https://accounts.google
    .com/ServiceLogin?service=mail&passive=true&rm=false&continue=https://mail.google.com/mail/&ss=1&scc=1&ltmpl=default&ltmplcache=2',
    'url_with_wrong_symbol' => 'www.g!ogle.com',
);

$validator->AddDataContainer($data);
// normal
$validatorType = new ValidatorTypeUrl('url_normal');
$validator->AddType($validatorType);

// url with properties
$validatorType = new ValidatorTypeUrl('url_with_properties');
$validator->AddType($validatorType);

// contains wrong symbol
$validatorType = new ValidatorTypeUrl('url_with_wrong_symbol');
$validator->AddType($validatorType);
$validator->Validate();

if($validator->GetHasErrorStatus()){
    $errors = $validator->GetErrorArray();
    foreach ($errors as $error){
        echo "<b>'{$error->GetFieldName()}'</b> - ".$error->ToString().'<br>';
    }
}
Return current item: ApPHP DataValidator Basic