Skip to content


Subversion checkout URL

You can clone with
Download ZIP
This class was created in an attempt to allow developers to create custom validation rules on single data fields or entire forms.
Branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.


>>> PHP Form Validation Class <<<

 Author: Rik Davis
Version: 1.0.0
   Date: 02-08-2012


This class was created with versatility in mind as it can easily be used in numerous project types.
The class itself requires use of an external rules file. The rules file is where all validation rulesets are defined.


A sample rules file and an index.php file is part of this repository to allow you to be able to more readily examine
how to implement the class.

The rule structure is defines as follows:

$rules[<set name>] = array(
       string                              int                      string                               string
  <form field name> = array('required' => <0|1>, 'field' => <human readable label>, 'regex' => <regex statement for field>)


<set name>         -  string representing the ruleset
<form field name>  -  string representing actual name of the form field as defined in the form input tags
'required'         -  boolean where 0 = form field value is not required, 1 = field value is required
'field'            -  represents the human readable string you can use for reporting validation errors to the user
'regex'            -  string containing regex field rule. *See regex note below*

*regex - The regex string you create should not contain beginning & ending slashes as they will be put added in the class.
         Additionally, you should always start each regex statement with the caret character ^ and end with the dollar sign character $

         An example of a regex statement might be; 'regex' => '^[A-Za-z0-9_-]$'


The class can be instantiated in two ways;
1) With path to rules file    - $val = new Validate('/path/to/rules/file'); (filename should include '.php' extension)
2) Without path to rules file
   - $val = new Validate();
   - $val->load_rules_file('/path/to/rules/file');

Both cases will load all defined rules into the class object.

Once you have instantiated the class and loaded the rules file using one of the two above listed methods, validation is very
easy to perform. (examples below are using the $val object for reference)


You can either validate a single field manually by doing the following:

  $result = $val->validate_field(string ruleset, string field, mixed value[, bool case_insensative = true]);

Or validate an entire form as follows;

  $result = $val->validate_form(string ruleset, array form post data[, case_insensative = true]);

In both cases, the result you will get back will be a results object defined as follows:

Results Object
    [valid] => Array
            [some_field_name] => 1
            [another_field_name] => 1
            [...etc] => 1

    [invalid] => Array

    [missing] => Array

    [errors] => Array
           [email_addr] => 'No such rule exists for this field'


All of the array name/value pairs in the Results objects are booleans except for the errors element.
If errors contains any key/value elements, the values will be strings containing a string of the error message.


As a programmer, you have two ways you can use to react to validation results:
1) The class has a method called validation_status() which takes no arguments and returns a simple boolean
   result if the validation passed(1) or failed(0).

2) The preferred way to use this class would be to actually check for the presence of elements in the
   missing, invalid & error arrays with using a simple count() function call and react based on the result of
Something went wrong with that request. Please try again.