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

Important!

Dear visitors,

This blog and the Tigarus.com network will be changing our website software in May (2024). There will be some down time and there will be a change to the webpage addresses.

We will also be removing the Anime blogs. The Rocksmith Songs website will be unavailable of a few weeks or months while I move the database to the new software.

At the moment we are running on Wordpress, the new software is an in-house project which is 57 times faster. It will also provide more freedom for development.

Thank you for understanding.

, 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.

Advertisement

Donate Now!

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


Method:

_REQUEST_VALIDATOR();

// 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

name

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

type

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

minLength

// Set the minimum input length

maxLength

// Set the maximum input length

minNumber

// Set a minimum number required, for numeric values

maxNumber

// Set a maximum number limit, for numeric values

test

// Test the input with a regular expression

required

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

defaultValue

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

clip

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

options

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

error

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

value

// Set the value; returns the processed value

Example Usage

$obj = new _REQUEST_VALIDATOR();

$obj->add(‘first’);
$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() ): ?>

<fieldSet><legend>Errors:</legend>

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

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

} ?>

</fieldSet>

<?php endif; ?>

Get Value

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

 

Advertisement

Donate Now!

Hey buddy, it takes a lot to make a blog. How about a donation?


 

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.