My Validation Class
The class
Download My Validation Class From http://github.com/harake
This is a very basic class that allows you to add more public static functions to validate more data types other than integer, boolean, decimal, and ids
How To Use It
Currently we have 4 public static methods
- MyValidation::ValidateBoolean();
- MyValidation::ValidateInteger();
- MyValidation::ValidateDecimal();
- MyValidation::ValidateIds();
Every method returns an array of values or empty array on failure.
Every method can accept any of these sets of consecutive arguments
- first set
- $value_or_array_of_values
- second set
- $value_or_array_of_values
- $array_of_possible_values
- third set
- $value_or_array_of_values
- $array_of_possible_values
- $skip_invalid_values
- fourth set
- $value_or_array_of_values
- $array_of_possible_values
- $skip_invalid_values
- $min_acceptable_number_of_values
- fifth set
- $value_or_array_of_values
- $array_of_possible_values
- $skip_invalid_values
- $min_acceptable_number_of_values
- $max_acceptable_number_of_values
Examples
// To validate if the value 3 is an integer
MyValidation::ValidateInteger(3) MyValidation::ValidateInteger(array(3))
// To validate if the value 3 is an integer that is either 3 or 4
MyValidation::ValidateInteger(3, array(3, 4)) MyValidation::ValidateInteger(array(3), array(3, 4))
// To validate if the array(3, 4, 5) is an array of integers
// where every value is either 3 or 4 or 5 or
MyValidation::ValidateInteger(array(3, 4, 5), array(3, 4, 5, 6))
// To validate if the array(3, 4, 5, ‘k’) is an array of integers
// and skip invalid integers
MyValidation::ValidateInteger(array(3, 4, 5, 'k'), null, true);
// To validate if the array(3, 4, 5, ‘k’) is an array of integers
// where every value is either 3 or 4 or 5 or 6 and skip invalid integers
MyValidation::ValidateInteger(array(3, 4, 5, 'k'), array(3, 4, 5, 6), true)
// To validate if the array(3, 4, 5, 6, ‘k’) is an array of integers
// and skip invalid integers but must have at least 2 valid integers
MyValidation::ValidateInteger(array(3, 4, 5, 6, 'k'), null, true, 2)
// To validate if the array(3, 4, 5, 6, ‘k’) is an array of integers
// and do not skip invalid integers but must have at least 2 valid integers
MyValidation::ValidateInteger(array(3, 4, 5, 6, 'k'), null, false, 2)
// To validate if the array(3, 4, 5, 6, ‘k’) is an array of integers
// and skip invalid integers but must have at most 3 valid integers
MyValidation::ValidateInteger(array(3, 4, 5, 6, 'k'), null, true, null, 3)
Please leave a comment, If you like this article or have one or more remark(s) on it
Advertisement
works like a charm, but few validation data types are still missing.
How to add new function, let us say validateEmail() function?
This class is made to make you extends it by adding new functions for each data type
If you read the class, you find this
public static function validateInteger() {
extract(self::__correct(func_get_args()), EXTR_OVERWRITE);
$values = self::__general_stage1($values, $possible, $filter, $min, $max);
return self::__general_stage2('/(^0$)|(^\-?[1-9]\d*$)/', $values, $filter, $min, $max);
}
just mimic it and replace the
'/(^0$)|(^\-?[1-9]\d*$)/'regular expression with the regular expression of an email addresspublic static function validateEmail() {
extract(self::__correct(func_get_args()), EXTR_OVERWRITE);
$values = self::__general_stage1($values, $possible, $filter, $min, $max);
return self::__general_stage2($put_here_the_new_regular_expression, $values, $filter, $min, $max);
}