Ok had a rethink about how things were occuring.
Would appreciate any more comments on how I could tidy things up more and make them flexible.
The class now looks like this:
<?php
class formValidation {
// Set Default Variables
var $errors; # Error Reporting
// Check for an empty string
function isEmpty($field, $string)
{
if(empty($string)) {
$this->errors[] = 'The '.$field.' is empty.';
} else {
return true;
}
}
// Check for a string is a string
function isString($field, $string)
{
if(is_string($string)) {
return true;
} else {
$this->errors[] = 'The '.$field.' is not a string.';
}
}
// Check a string is numeric
function isNumeric($field, $string)
{
if(is_numeric($string)) {
return true;
} else {
$this->errors[] = 'The '.$field.' is not numeric.';
}
}
// Compare 2 strings
function strCompare($field, $string1, $string2)
{
if((trim($string1)) != (trim($string2))) {
$this->errors[] = 'The '.$field.'s do not match.';
} else {
return true;
}
}
// Check a string against a maximum length
function maxLength($field, $string, $length)
{
if(strlen($string) > (int)$length) {
$this->errors[] = 'The '.$field.' is greater than '.(int)$length.' character maximum limit.';
} else {
return true;
}
}
// Check a string against a minimum length
function minLength($field, $string, $length)
{
if(strlen($string) < (int)$length){
$msg = "string is greater than minimum length allowed";
$this->errors[] = 'The '.$field.' is has fewer characters than the '.(int)$length.' charcter minimum limit';
} else {
return true;
}
}
// Compare string against a regular expression
function regEx($field, $string, $regex)
{
if(!is_string($string)){
return false;
} else {
switch($regex){
case 'az': # Basic az lowercase character class
$rule = '^[a-z]*$';
$error = 'The '.$field.' can only contain lowercase letters';
break;
case 'aZ': # Lower & uppercase character class
$rule = '^[A-Za-zÀ-ÖØ-öø-ÿ]*$';
$error = 'The '.$field. ' can only contain letters';
break;
case 'num': # Numbers 0-9
$rule = '^[0-9]*$';
$error = 'The '.$field. ' can only contain letters and numbers';
break;
case 'alphanum': # Alpha-numeric all basic characters and letters
$rule = '^[A-Za-zÀ-ÖØ-öø-ÿ0-9]*$';
$error = 'The ' . $field. ' can only contain letters and numbers';
break;
case 'files': # Filename
$rule = '^[a-zA-Z_-]{1}[a-zA-Z0-9_-]*\.[a-zA-Z0-9]{1, 3}$';
$error = 'The '.$field.' has to start with a letter and end with a full stop and the file type extention (e.g. .txt), as well as only contain letters, numbers and the following characters: ., _, -';
break;
case 'name': # Name
$rule = '^[A-Za-zÀ-ÖØ-öø-ÿ0-9 \'`’_-]*$';
$error = 'The '.$field." can only contain letters, numbers and the following characters: ' ', ', `, ’, _, -";
break;
case 'text': # Basic text
$rule = "^[A-Za-zÀ-ÖØ-öø-ÿ0-9\r\n \.,\?!:\(\)/'`’&_-]*$";
$error = 'The '.$field. " can only contain letters, numbers and the following characters: ' ', ., ',', ?, !, :, (, ), /, ', `, ’, &, _, -";
break;
case 'phone': # Telephone Numbers
$rule = '^[0-9 \+\(\)]*$';
$error = 'The '.$field." can only contain numbers and the following characters: ' ', +, (, )";
break;
case 'postcode' :
$rule = '^[a-zA-Z0-9A-Za-zÀ-ÖØ-öø-ÿ _-]*$';
$error = 'The '.$field." can only contain letters, numbers and the following characters: ' ', _, -";
break;
case 'email': # Email address
$rule = '^[A-Za-z0-9\._-]+@[A-Za-z0-9\.-]+\.[A-Za-z]{2,4}$';
$error = 'The '.$field.' does not match the standard email format';
break;
default:
return false;
}
if(eregi($rule, $string)){
return true;
} else {
$this->errors[] = $error;
}
}
}
}
I'm now calling the object like so. My only concern now is the methods array I have which calls the methods required. Any ideas of how to deal with this in a more effective manner??
<?php
// Include validation class
include 'includes/validate2.php';
// Instantiate a new formValidation object
$validate = new formValidation();
// Test Array - $_POST can be used
$input = array('firstname' => 'a', 'surname' => 'rrr', 'postcode' => 'ls12 7rt');
// Define array for form elements to be tested against
$methods = array('firstname' => array('isEmpty' => '', 'regEx' => 'aZ'),
'surname' => array('isEmpty' => '', 'regEx' => 'aZ'),
'company' => array('isEmpty' => '', 'regEx' => 'aZ'),
'address1' => array('isEmpty' => '', 'regEx' => 'aZ'),
'address2' => array('isEmpty' => '', 'regEx' => 'aZ'),
'town' => array('isEmpty' => '', 'regEx' => 'aZ'),
'county' => array('isEmpty' => ''),
'postcode' => array('isEmpty' => '', 'regEx' => 'postcode'),
'tel' => array('isNumeric' => '', 'regEx' => 'phone')
);
// Loop through the values being passed from the $input array
// Test to see if the $input array key matches the key in the methods arrays.
//
// IF this evaluates to true then loop through that specific dimension and
// use the key to call the method using the switch statement, passing in
// the values from the $input array and also the values to test against.
//
foreach($input as $key => $value){
if(array_key_exists($key, $methods)){
foreach($methods[$key] as $v_key => $v_value){
switch($v_key){
case isEmpty:
$validate->isEmpty($key, $value);
break;
case isString:
$validate->isString($key, $value);
break;
case isNumeric:
$validate->isNumeric($key, $value);
break;
case maxLength:
$validate->maxLength($key, $value, $v_value);
break;
case minLength:
$validate->minLength($key, $value, $v_value);
break;
case regEx:
$validate->regEx($key, $value, $v_value);
break;
default:
break;
}
}
}
}
// Finally access the errors variable within the object and output any errors
// which have occured whilst looping through the values.
if(count($validate->errors) > 0){
foreach($validate->errors as $error){
echo '<li>'.$error.'</li>';
}
}