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.

Advertisement


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


 

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.

 

Matched Content