This blog uses responsible cookies for functionality and user preferences.
Google Analytics is used for collecting general public information to understand how my blog is used.
Learn moreName: 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.
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’); ?>” />
Terms of Use, Cookies, Privacy, and more, see Terms and Privacy
Third-party copyrights and trademarks featured on this site are owned by their respected holders.
Website written and designed by Patcoola 2019, some rights reserved.
This blog uses responsible cookies for functionality and user preferences.
Google Analytics is used for collecting general public information to understand how my blog is used.
Learn moreDear EU visitor, I am required to ask you consent for the following.