Hussein Harake

My Validation Class

Posted in PHP by harake on February 7, 2010

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

  1. MyValidation::ValidateBoolean();
  2. MyValidation::ValidateInteger();
  3. MyValidation::ValidateDecimal();
  4. 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

  1. first set
    1. $value_or_array_of_values
  2. second set
    1. $value_or_array_of_values
    2. $array_of_possible_values
  3. third set
    1. $value_or_array_of_values
    2. $array_of_possible_values
    3. $skip_invalid_values
  4. fourth set
    1. $value_or_array_of_values
    2. $array_of_possible_values
    3. $skip_invalid_values
    4. $min_acceptable_number_of_values
  5. fifth set
    1. $value_or_array_of_values
    2. $array_of_possible_values
    3. $skip_invalid_values
    4. $min_acceptable_number_of_values
    5. $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

2 Responses

Subscribe to comments with RSS.

  1. IBRAHAM said, on February 7, 2010 at 7:05 pm

    works like a charm, but few validation data types are still missing.
    How to add new function, let us say validateEmail() function?

  2. harake said, on February 7, 2010 at 7:35 pm

    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 address


    public 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);
    }


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.