The Official Blog of Patcoola

Im just a dude watching over my parents,
and trying to make cartoons and video games from home.

PHP: Request Validator

, By Patcoola

Name: Request Validator
Programming Language: PHP
Date Created: 2021-02-19

Purpose: Provides a clean and simple way to validate GET and POST requests with PHP.

Description: Request Validator is a class for PHP to simplify the validation of GET and POST requests. Process and validate requests by creating definitions. Log and display errors made easy.


Donate Now!

This might not be the best time, but making a blog is not easy. Would you consider donating?



// Class Name

add ( name )

// Function to create an entry

set ( name, property, value )

// Function to set a definition

get ( name, property )

// Function to return the value of a property, use to get the processed value

checkRequired ( method, array )

// Function to check if all required are met, returns true or false. Use debug to see if a required was not found

validate ( method, array )

// Function to process a validate the requests

debugMode ( bool )

// Function to determine if to include extra error logging. Used with check required, reports required which were not given.

hasErrors ()

// Returns true if there is an error

errorCount ()

// Returns the number of errors counted

getErrors ()

// Returns an array of error messages

Set and Get Properties


// Set a label for the error logger, default is the key name


// Set the data type: string, url, email, int, float, bool


// Set the minimum input length


// Set the maximum input length


// Set a minimum number required, for numeric values


// Set a maximum number limit, for numeric values


// Test the input with a regular expression


// Set to true if required, default false; Use with checkRequired function


// Set a default value, used with Get function with the value property


// Set to true if you wish to clip the input with length or number properties, default false


// Test with a string of values separated with the pipe delimiter


// Set to true if there is an error, default false; Returns boolean


// Set the value; returns the processed value

Example Usage

$obj = new _REQUEST_VALIDATOR();

$obj->set(‘first’, ‘name’, ‘First Name’);
$obj->set(‘first’, ‘type’, ‘string’);
$obj->set(‘first’, ‘minLength’, 0);
$obj->set(‘first’, ‘maxLength’, 32);
$obj->set(‘first’, ‘test’, “/[A-Za-z’-]/”);
$obj->set(‘first’, ‘required’, true);

if ( $_SERVER[‘REQUEST_METHOD’] === ‘POST’ ) {

if ( $obj->checkRequired(“POST”, $_POST) ) {

$obj->validate(“POST”, $_POST);



Errors Usage

<?php if ( $obj->hasErrors() ): ?>


<?php foreach ( $obj->getErrors() as $msg ) {

ECHO “<p>$msg</p>”;

} ?>


<?php endif; ?>

Get Value

<input type=”text” name=”first” value=”<?php ECHO $obj->get(‘first’, ‘value’); ?>” />



Donate Now!

Hi there, I spend a lot of time making this blog.

It would be great if you would consider donating.


Post Meta

Share Post


Comments: Write Comment

Be the first to write a comment.

Leave a Comment

All comments are reviewed before publishing. Comments must be related to the page topic, must not be spam, and must comply with the criminal code of Canada.