Development Information

December 20, 2009

Input Validation Class

Filed under: Validation Class Information — judda @ 6:49 pm

As mentioned yesterday, I started working on an input validation class in an attempt to make the validation of all input less tedious.  The first version of this class is complete.

Currently there are only four types of information which can be validated through this, but as time rolls on, I hope to add more to it making it more and more useful.

For starters, I would strongly suggest reading the documentation about the ‘validate’ function (click here for more information).

The following are the data types which are available:

  • length – check the minimum and/or maximum length of the input data
  • string - specify specific information about the actual string
  • integer - a number without any decimal points (defaults to signed)
  • decimal - a number with decimal places (defaults to signed)
  • real – same as decimal

For the data to be validated, the following two fields must also be present:

  • value – the value which needs to be validated
  • field - the name that this data will be referenced by

For each of the different types of checks, there are extra parameters (some optional, some required) which can help to ensure that your data is exactly what you want it to be.

The extra parameters are as follows:

length (a minimum of 1 of these parameters must be provided):

  • min – The minimum length that the value is allowed to have
  • max – The maximum length that the value is allowed to have

string (a minimum of 1 of these parameters must be provided):

  • alphabetic – Only allows letters of the alphabet in the text
  • alphanumeric – Only allows letters of the alphabet and numbers in the text
  • anything – Allows any kind of characters in the string
  • spaces – Allows for spaces to be in any of the text fields (combines with the three previous options)

integer:

  • min – The minimum value that the number is allowed to be
  • max – The maximum value that the number is allowed to be

decimal/real:

  • min – The minimum value that the number is allowed to be
  • max – The maximum value that the number is allowed to be

You may be wondering why this is all done in a class.   It is true that a class is not necessary however, it makes accessing it relatively convenient.  In order to reduce the overhead slightly, the function you call is static.  This means that you are able to access the function from outside of it, instead of needing to instantiate a copy of it before using the validate function.

How to use:

Step 1:

Include the validation class in the page (so PHP knows what it is).

require_once (/path/to/database/class/validate.php”);

Step 2:

Set up the arrays for the validation (this step can be skipped if you don’t mind placing arrays into the function call).

// outcome is the fact that the text provided is greater than the maximum
// allowed length as well as there is a number (it is alphanumeric not alphabetic)
// and that there is a space in the text
$validate1 = array (
	'value' => 'bo bo1',
	'field' => 'asdf',
	'length' => array ( 'max' => 3 ),
	'string' => array ( 'alphabetic' )
);
// outcome is the fact that the value is less than the minimum value
$validate2 = array (
	'value' => 5.5,
	'field' => 'asdf',
	'real' => array ( 'min' => 5.6, 'max' => 5.7 )
);

Step 3:

Call the validation function.  The validation function will return one of two things:

  • array – An array of issues which were found in the data provided
  • NULL – If no issues were found, meaning that the data meets the requirements
// Assuming that the variables from Step 2 were created
$issues = ValidateText::validate ( $validate1, $validate2 );
 
if ( $issues == NULL ) // Check if there were any issues
{
	echo ( 'No issues found.' );
}
else
{
	// Errors found, just do a quick dump of them all (you should deal with them differently)
	print_r ( $issues );
}

You have found all of the issues which were with the input data!

Please Note: You are able to provide the validate function with as many parameters as you desire, and it will process them all.  The output will be the accumulation of all issues which were found with the data.

Please let me know if you have any questions, comments or suggestions about this script and what else should be added into it.

Powered by WordPress