PHP Classes

PHP Validator: Validate request values with given rules

Recommend this page to a friend!
  Info   View files Example   Screenshots Screenshots   View files View files (27)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2024-06-15 (Yesterday) RSS 2.0 feedNot enough user ratingsTotal: 205 This week: 2All time: 8,429 This week: 35Up
Version License PHP version Categories
php-validator 1.7MIT/X Consortium ...5.4PHP 5, Databases, Validation
Description 

Author

This class can validate request values with given rules.

It can take an array of POST or GET request values and validate against a list of given rules.

If the validation fails, the class returns a list of error objects that can return the error details for each of the request value.

Currently the class supports validating values with rules that check required values, minimum and maximum length, email addresses, url, ip address, letters and digits, numbers, regular expression, unique value database field.

Picture of Ravi Kumar
  Performance   Level  
Name: Ravi Kumar <contact>
Classes: 6 packages by
Country: India India
Innovation award
Innovation award
Nominee: 2x

Example

<?php

/**
 * This file is only used for demo purpose.
 *
 * @package Validator
 * @author Ravi Kumar
 * @version 0.1.0
 * @copyright Copyright (c) 2014, Ravi Kumar
 * @license https://github.com/ravikumar8/PHP-Validator/blob/master/LICENSE MIT
 **/

require_once 'classes/Database.php';
require_once
'classes/ErrorHandler.php';
require_once
'classes/Validator.php';

$db = new Database;
$errorHandler = new ErrorHandler;
$errosHtml = '';

if(!empty(
$_POST)) {

   
$validator = new Validator($db, $errorHandler);

   
$validation = $validator->check($_POST, [
       
'username' => [
           
'required' => true,
           
'maxlength' => 20,
           
'minlength' => 3,
           
'alnum' => true,
           
'unique' => 'users'
       
],
       
'email' => [
           
'required' => true,
           
'maxlength' => 255,
           
'email' => true,
           
'unique' => 'users'
       
],
       
'password' => [
           
'required' => true,
           
'minlength' => 7
       
],
       
'password_again' => [
           
'matches' => 'password'
       
]
    ]);
   
    if(
$validation->fails() ) {

       
//echo '<pre>', print_r( $validation, 1 ), '</pre>';

       
if( $validation->errors()->hasErrors('username') ) {
           
$errosHtml = '<li>' . implode( '</li><li>' , $validation->errors()->all('username') ) . '</li>';
        }
        if(
$validation->errors()->hasErrors('email') ) {
           
$errosHtml .= '<li>' . implode( '</li><li>' , $validation->errors()->all('email') ) . '</li>';
        }
        if(
$validation->errors()->hasErrors('password') ) {
           
$errosHtml .= '<li>' . implode( '</li><li>' , $validation->errors()->all('password') ) . '</li>';
        }
        if(
$validation->errors()->hasErrors('password_again') ) {
           
$errosHtml .= '<li>' . implode( '</li><li>' , $validation->errors()->all('password_again') ) . '</li>';
        }
    }
}
?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Validation</title>
    <link rel="stylesheet" href="assets/css/main.css">
</head>
<body>

    <div class="container">
        <?php if( $errosHtml ) { ?>
<ul class="alert error">
                <?php echo $errosHtml; ?>
</ul>
        <?php } ?>
<form action="index.php" method="post" auto-fill="off">
            <ul>
            <li>Username:<input type="text" name="username" value="<?php echo isset($_POST['username']) ? $_POST['username'] : ''; ?>"></li>
            <li>Email:<input type="email" name="email" value="<?php echo isset($_POST['email']) ? $_POST['email'] : ''; ?>"></li>
            <li>Password:<input type="password" name="password"></li>
            <li>Password again:<input type="password" name="password_again"></li>
            <li><input type="submit" class="btn"></li>
            </ul>
        </form>
    </div>
</body>
</html>


Details

PHP Validator

Validator class to validate form post values in a simple way.

Usage


require_once 'classes/Database.php';
require_once 'classes/ErrorHandler.php';
require_once 'classes/Validator.php';

$db             =   new Database;
$errorHandler   =   new ErrorHandler;
$errosHtml      =   '';

if(!empty($_POST))  {

    $validator = new Validator($db, $errorHandler);

    $validation = $validator->check($_POST, [
        'username'  =>  [
            'required'  =>  true,
            'maxlength' =>  20,
            'minlength' =>  3,
            'alnum'     =>  true,
            'unique'    =>  'users'
        ],
        'email' =>  [
            'required'  =>  true,
            'maxlength' =>  255,
            'email'     =>  true,
            'unique'    =>  'users'
        ],
        'password'  =>  [
            'required'  =>  true,
            'minlength' =>  7
        ],
        'password_again'    =>  [
            'matches'   =>  'password'
        ]       
    ]);
    
    if( $validation->fails() )  {

        //echo '<pre>', print_r( $validation, 1 ), '</pre>';

        if( $validation->errors()->hasErrors('username') )  {
            $errosHtml  = '<li>' .  implode( '</li><li>' ,  $validation->errors()->all('username') ) . '</li>'; 
        }
        if( $validation->errors()->hasErrors('email') ) {
            $errosHtml  .= '<li>' .  implode( '</li><li>' ,  $validation->errors()->all('email') ) . '</li>';   
        }
        if( $validation->errors()->hasErrors('password') )  {       
            $errosHtml  .= '<li>' .  implode( '</li><li>' ,  $validation->errors()->all('password') ) . '</li>';    
        }
        if( $validation->errors()->hasErrors('password_again') )    {       
            $errosHtml  .= '<li>' .  implode( '</li><li>' ,  $validation->errors()->all('password_again') ) . '</li>';  
        }       
    }
}

<?php if( $errosHtml )  { ?>
    <ul class="alert error">
        <?php echo $errosHtml; ?>
    </ul>       
<?php } ?>

Rules

* __required__: Returns FALSE if the form element is empty. * __minlength__: Returns FALSE if the form element is shorter then the parameter value. minlength=>6 * __maxlength__: Returns FALSE if the form element is longer then the parameter value. maxlength=>10 * __email__: Returns FALSE if the form element does not contain a valid email address. * __activeemail__: Returns FALSE if the form element does not contain a valid and active email address. * __url__: Returns FALSE if the form element does not contain a valid url address. * __activeurl__: Returns FALSE if the form element does not contain a valid and active url address. * __ip__: Returns FALSE if the supplied IP is not valid. * __alpha__: Returns FALSE if the form element contains anything other than alphabetical characters. * __alphaupper__: Returns FALSE if the form element contains anything other than upper alphabetical characters. * __alphalower__: Returns FALSE if the form element contains anything other than lower alphabetical characters. * __alphadash__: Returns FALSE if the form element contains anything other than alpha-numeric characters, underscores or dashes. * __alphanum__: Returns FALSE if the form element contains anything other than alpha-numeric characters. * __hexadecimal__: Returns FALSE if the form element contains anything other than hexadecimal characters. * __numeric__: Returns FALSE if the form element contains anything other than numeric characters. * __matches__: Returns FALSE if the form element does not match the one in the parameter. matches[form_item] * __unique__: Returns FALSE if the form element is not unique to the table and field name in the parameter. unique[field]

Based on Alex Garrett work http://bit.ly/1oO8Yxn

License

Released under the MIT license<br> Copyright (c) 2014 Ravi Kumar


Screenshots  
  • Screenshot.png
  • With_Error.png
  Files folder image Files  
File Role Description
Files folder imageassets (3 directories)
Files folder imageclasses (3 files)
Accessible without login Plain text file index.php Example Example script
Accessible without login Plain text file LICENSE Data Auxiliary data
Accessible without login Plain text file README.md Doc. Auxiliary data

  Files folder image Files  /  assets  
File Role Description
Files folder image.sass-cache (4 directories)
Files folder imagecss (1 file)
Files folder imagescss (1 file, 3 directories)

  Files folder image Files  /  assets  /  .sass-cache  /  26ee63337328dd3a18b721f6acef71df44d78233  
File Role Description
  Accessible without login Plain text file main.scssc Data Auxiliary data

  Files folder image Files  /  assets  /  .sass-cache  /  3e46442c5c1a597a2be3d51d7957d250e531cf10  
File Role Description
  Accessible without login Plain text file _all.scssc Data Auxiliary data
  Accessible without login Plain text file _typography.scssc Data Auxiliary data

  Files folder image Files  /  assets  /  .sass-cache  /  abd329f8a58e0ae78a565b4202de3890a2e92050  
File Role Description
  Accessible without login Plain text file _all.scssc Data Auxiliary data
  Accessible without login Plain text file _functions.scssc Data Auxiliary data
  Accessible without login Plain text file _mixins.scssc Data Auxiliary data
  Accessible without login Plain text file _variables.scssc Data Auxiliary data

  Files folder image Files  /  assets  /  .sass-cache  /  f50cb1c89ab60f829126d7719fa187eebcee1984  
File Role Description
  Accessible without login Plain text file _alerts.scssc Data Auxiliary data
  Accessible without login Plain text file _all.scssc Data Auxiliary data
  Accessible without login Plain text file _buttons.scssc Data Auxiliary data

  Files folder image Files  /  assets  /  css  
File Role Description
  Accessible without login Plain text file main.css Output Sample output

  Files folder image Files  /  assets  /  scss  
File Role Description
Files folder imagebase (2 files)
Files folder imagehelpers (4 files)
Files folder imagemodules (3 files)
  Accessible without login Plain text file main.scss Output Sample output

  Files folder image Files  /  assets  /  scss  /  base  
File Role Description
  Accessible without login Plain text file _all.scss Data Auxiliary data
  Accessible without login Plain text file _typography.scss Data Auxiliary data

  Files folder image Files  /  assets  /  scss  /  helpers  
File Role Description
  Accessible without login Plain text file _all.scss Data Auxiliary data
  Accessible without login Plain text file _functions.scss Data Auxiliary data
  Accessible without login Plain text file _mixins.scss Data Auxiliary data
  Accessible without login Plain text file _variables.scss Data Auxiliary data

  Files folder image Files  /  assets  /  scss  /  modules  
File Role Description
  Accessible without login Plain text file _alerts.scss Data Auxiliary data
  Accessible without login Plain text file _all.scss Data Auxiliary data
  Accessible without login Plain text file _buttons.scss Data Auxiliary data

  Files folder image Files  /  classes  
File Role Description
  Plain text file Database.php Class Class source
  Plain text file ErrorHandler.php Class Class source
  Plain text file Validator.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 93%
Total:205
This week:2
All time:8,429
This week:35Up