FORM VALIDATION CLASS - SASA SAVIC
Now let's assume we have a simple form like this one:
<form action="" method="post">
<input type="text" name="test" value="" />
<input type="submit" name="submit" value="Submit" />
</form>
We want to set some rules that need to be passed if user wants to submit their form.
Let the rules be:
- Field `test` is required
- Field `test` must be at least 10 characters long
- Field `test` must not be more than 30 characters long
- Field `test` must be a valid URL.
- And we want to remove white space from end of the value
Now lets see how we define our rules.
First of all we need to load the class and make an instance of the class :
<?php
//include form validation file
include 'path/to/class/file.php';
//make an instance of class
$FV = new Form_validation;
//And thats it for that issue.
About method that set rules to validate
//Method that allows us to set rules for fields is 'Form_validation::set_rules()'
//we use it like
$FV->set_rules('field_name', 'field_label', 'string_with_rules');
//where:
//`field_name` is index in $_POST array
//`field_label` is how we will call that field when we notify user about errors
//`string_with_rules` is a string that contains set of rules divided by pipe (|) sign
Lets create string with rules for our set of rules
//our field is required so thats the first rule
$rules = 'required'; //no need to set pipe as we have just one rule
//It must be at least 10 characters long, we use 'min_length' rule
//so our string will now look like
$rules = 'required|min_length[10]'; //parameters are set in square brackets
//Field must be shorter that 30 characters. We use 'max_length' rule
//now our rules string looks like this
$rules = 'required|min_length[10]|max_length[30]';
//Last thing on our list is not validation rule, its more like preparation of data
//and yes that is allowed in this script, you can use any function to modify data that php has
//with one rule, it must not require more that one variable (like htmlentities,trim,...)
// , as only variable that will be passed is posted data
//its recommanded to call that preparations before any validation is done. So now our string look like this
$rules = 'trim|required|min_legth[10]|max_length[30]';
Now we can use our rules string to validate form
//set the rules
$FV->set_rules('test', 'TEST' , $rules);
// we will check if form is valid
if ( $FV->validate_form() )
{
//method returns TRUE is form is valid
echo "FORM IS VALID"
}
else
{
//if false is returned then we have errors, you can display them via
echo $FV->return_errors();
}
And done :) .
Class allows you to easily use different language files other that specified one. You just translate the file and load it via instantiated object.
//load new language file
$FV->load_lang_file('path/to/lang/file.php');
You can make your own prepareation functions.
//first create a function that accepts one parameter
//this function will remove all dashes from string
function delete_dashes( $field_value = '' )
{
return str_replace('-', '', $value);
}
//now when we set `delete_dashes` preparation it will run that function
$FV->set_rules('test', 'Test', 'delete_dashes|required');
If you want your functions to accept a parameter you can always extend class and make a method that will do what you want. But beware method must return TRUE on success or FALSE on fail, nothing other (thats for now, I have some plans )
class My_FV extends Form_validation
{
//length must be equal to number in parameter
public function equal_length(field_value, $parameter)
{
return ( $parameter === strlen($field_value) ) ? TRUE : FALSE;
}
}
//now you can use 'equal_length[XXXXX]' rule where 'XXXXX' is number that will represent length you want
//beware that you must create index in language file that will show error, you can check the pattern in language file
And that would be it for this readme file, if you want to know more open the script its commented. Script is dedicated to my friend
Jay Sean ( softwarefreak ).