EasyValidation Manual:
Author: Marvin Petker
Version: 1.0
Date: 2014-09-23
License: GNU General Public License 3.0
License URL: http://www.gnu.org/licenses/gpl-3.0.txt
Description: This class is used for easy validation of input data.
Note: The Idea for that class came from phpacademy.org and their youtube channel.
I've changed some things but I recommend you to watch their tutorials because they're very good and
can give beginners an understanding of oop and other stuff.
Table of Contents:
0. Introduction and requirements Line 13
1. Defining rules Line 23
2. Defining error messages Line 50
3. Validating Line 88
4. Extending the class Line 116
5. Available rules Line 150
-------------------------------------------------------------------
0. Introduction and requirements
Hello World,
I wrote this class to create an easy, extendable solution for validating user input.
As I wrote in the class comment I got inspired by www.phpacademy.org and I wanna thank them.
To the class, I wrote it in PHP 5.5.3 but will test earlier versions soon.
To use the class simply add the class via require or an autoloader to your script.
DONT FORGET to wrap all actions of the EasyValidation class inside a try-catch-block.
-------------------------------------------------------------------
1. Defining rules
First before we start we have to define our rules to check.
Every rule has its own method and the class wil throw an exception if there is no
equivalent message.
All rules can be seen in 6. Possible rules.
We define our rules with an associative array, where our keys are the field the rules are applied to
and the rules themselfs are wrapped in another array with the rule as key=>value pairs.
!!!NOTE!!!
If rules for a field aren't defined the field will be ignored and not checked.
Example:
<?php
//After sending a form the $_POST can look like this:
$_POST = array("username"=>"test123", "password"=>"mysecretpass123", "email"=>"test@mail.de", "submit"=>"register");
//We now have an username, a password and an email field ind our post.
//Every rule key must apply to a key from the array to check.
$rules = array(
"username" => array("required"=>true, "minLength"=>5, "maxLength"=>20, "alnum"=>true),
"password" => array("required"=>true, "minLength"=>5, "maxLenght"=>40),
"email" => array("email"=>true)
)
?>
-------------------------------------------------------------------
2. Defining error messages
We have the possibility to set our custom error messages and this is also required.
We have to set an error message for ever rule we are checking for.
For the above example we need messages for required, minLength, maxLength, alnum, email.
We define messages in a similar way like the rules.
Now we use key=>value pairs in rule=>message format.
<?php
$errMsgs = array(
"required" => "The :field is required.",
"minLength" => "The :field must be of minimum :value chars.",
"maxLength" => "The :field and must be of maximum :value chars.",
"alnum" => "The :field should only contain alphanmerical chars.",
"email" => "No valid email entered."
)
?>
We the ability to use :field, :value placeholder. These are gonna be replaced with the field name (exmp. username)
and the value is the satisfier of the rule so for required it would be true (thats why you shouldnt do that)
and in minLength for the username it would be 5.
You can also set rulesets for a specific field/key like the following:
<?php
//Format: "#fieldname" => array("rule"=>"message");
//Special messages for username:
$errMsgs = array(
"required" => "The :field is required.",
"minLength" => "The :field must be of minimum :value chars.",
"#username" => array("required"=>"Please enter an username", "anotherrule"=>"message")
)
?>
Rules for a specific field will override regular rules.
If a message for a rule is missing in a specific ruleset the class will take the regular rule
-------------------------------------------------------------------
3. Validating and error handling
Now our rules and messages are defined we can validate like the following:
<?php
//Rules defined
//Messages defined
$validator = new EasyValidation($errMsgs);
$validator->check($_POST, $rules);
if( $e = $validator->getErrors() ){
//Errors occured
}
else{
//No errors occured
}
?>
Get Errors returns an associative array in the following format:
array(
"fieldname" =>
array(
"rule1" => "message",
"rule2" => "message"
...
)
)
-------------------------------------------------------------------
4. Extending the class
To add your own rules you can easily extend the class and add your own methods.
Every method that is added must accept at least one and maximum two parameters.
If we want to add the rule "url" our satisfier would be true and we would implement
it like the following:
<?php
class MyValidator extends EasyValidation{
//URL validation
protected function url($value){
return filter_var($value, FILTER_VALIDATE_URL);
}
//IP validation, rule would be for exmpl. "ip"=>"ipv6"
protected function ip($value, $type){
switch($type){
case "ipv4":
return filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);
break;
case "ipv6":
return filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6);
break;
}
}
//More of your custom rules
}
?>
-------------------------------------------------------------------
5. Available rules:
required => true
email => true
alnum => true
digit => true
alpha => true
bool => true
int => true
numeric => true
string => true
float => true
minLength => value
maxLength => value
pcre => pattern
|