PHP Classes

File: example/validation_string_numbers.php

Recommend this page to a friend!
  Classes of Everton da Rosa   PTK Validation   example/validation_string_numbers.php   Download  
File: example/validation_string_numbers.php
Role: Example script
Content type: text/plain
Description: Example script
Class: PTK Validation
Validate values of dates, files and directories
Author: By
Last change:
Date: 10 years ago
Size: 2,452 bytes
 

Contents

Class file image Download
<?php

/*
 * Copyright (C) 2014 Everton
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * This file is a simple example of use Ptk\utils\Validator for testing an validate strings and numbers.
 */

function show_validation($value, $rules){
    global
$onerror;
    if(\
Ptk\utils\Validator::data($value, $rules, true)){
        echo
"This value $value is VALID.".PHP_EOL;
    }else{
        echo
"This value $value is INVALID.".PHP_EOL;
        echo
"Invalid for: ".PHP_EOL;
       
        echo
join(', ', $onerror);
    }
}

try{
    require
'examples.inc.php';
   
   
//testing numbers
   
$value = 100;
   
$rules = array(
       
'type' => array('integer')//type must be integer
       
,'min' => 0//min value is 0
       
,'max' => 100//max value is 100
   
);
   
show_validation($value, $rules);
   
   
$value = 10.5;
   
$rules = array(
       
'type' => array('integer', 'double')//type must be integer, float or double (for gettype php function, double = float
       
,'min' => 0
       
,'max' => 100
   
);
   
show_validation($value, $rules);
   
   
$value = 21;
   
$rules = array(
       
'type' => array('integer')
        ,
'step' => 5//the value must be multiple of 5
   
);
   
show_validation($value, $rules);
   
   
//testing strings
   
$value = 'PTK is cool!';
   
$rules = array(
       
'type' => array('string')//value must be string
       
,'min' => 5//min lenght of value is 5 char
       
,'max' => 20//maxlenght of value is 20 char
   
);
   
show_validation($value, $rules);
   
   
$value = 'john';
   
$rules = array(
       
'type' => array('string')//value must be string
       
,'list' => array('mary', 'paul', 'john')//value must by one of list (case sensitive)
   
);
   
show_validation($value, $rules);
   
} catch (
Exception $ex) {
    echo
$ex->getMessage();
    exit(
$ex->getCode());
}