PHP Classes
elePHPant
Icontem

User Input Validator: Validate request values according to given rules

Recommend this page to a friend!
  Info   View files Documentation   View files View files (6)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2016-12-29 (15 hours ago) RSS 2.0 feedNot enough user ratingsTotal: 256 This week: 24All time: 7,373 This week: 35Up
Version License PHP version Categories
input-validator 1.2MIT/X Consortium ...5.4HTTP, PHP 5, Validation
Description Author

This class can validate request values according to given rules.

It can take a array with the definitions of rules to validate values passed via POST or GET.

The class can check either the $_GET or $_POST array to verify if the values satisfy the defined rules.

It performs the supported types of validations using regular expressions. Currently it supports the validations of alphabetic strings with spaces, numbers or symbols, just numbers, just symbols, number or symbols, phone numbers, boolean or binary values.

  Performance   Level  
Name: Gavin Gordon Markowski <contact>
Classes: 8 packages by
Country: Canada Canada
Innovation award
Innovation award
Nominee: 2x

Details

InputValidator v1.0.0

Build Status

Prologue

Quickly define and run validation check on user data provided upon either GET or POST requests.

Installation (via Composer)

composer require gavinggordon/input-validator

Include autoloader.php

include_once( __DIR__ . '/vendor/autoload.php' );

Examples

Set Rules

The rules array contains the validation rules for each input. Each input is equal to 1 array, and, in itself, contains the rules for its own validation.

The first parameter of an input's validation array is used to define the Regex pattern by which to validate its value against.

The second parameter, which is also an array, is used to define the string length of the value, noted by the first parameter as a minimum length, followed by a maximum length.

$rules = [
  [
    'alpha', [ 5,16 ]
  ],
  [
    'phone', [ 7,15 ]
  ],
  [
    'alpha_number_symbol_spaces', [ 2,155 ]
  ]
];

Create Instance

To instantiate the class, two arguments are required:

The first argument must be a string value of either 'GET' or 'POST', and is used to determine the request type being validated.

The second argument must be an array of rules, as mentioned in the above example.

$validator = new GGG\InputValidator( 'POST', $rules );

Run the Validator

$validated = $validator->validate();

Determine if Validated

// if passed validation 
if( $validated === true ) {

  // retrieve an associative array of inputs
  $data = $validator->getInputs();
  
}

// if has errors 
if( is_array( $validated ) ) {

  // show an array containing messages for
  // each input which did not pass validation.
  var_dump( $validated );
  
}

More Information

Feedback

If you found this class to be at all interesting, helpful, particularly useful, or innovative in any way, check out my other PHP classes via my GitHub profile or PHPClasses.org profile.

  Files folder image Files  
File Role Description
Files folder imagesrc (1 file)
Accessible without login Plain text file .travis.yml Data Auxiliary data
Accessible without login Plain text file composer.json Data Composer.json
Accessible without login Plain text file LICENSE.txt Doc. Documentation
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file README.md Doc. README

  Files folder image Files  /  src  
File Role Description
  Plain text file InputValidator.php Class Main class file

 Version Control Unique User Downloads Download Rankings  
 100%
Total:256
This week:24
All time:7,373
This week:35Up