Requirements

  • PHP Version => 5.3+
  • MySQLi (Optional)

Installation

Upzip the package and copy Validx.php from src directory in your project. Then include it with your scripts.

Basic Usages

Validx class validating data and retriving error messages. If all rules passed successfully then return true;


//Here is our first demo's code

require_once 'path/to/Validx.php';
$rules=[
  'name'=>'required||min:5',
  'email'=>'required||email',
  'password'=>'required||same:repassword',
  'dob'=>'required||date||date_min:1997-01-01',
  'photo'=>'image',
  'website'=>'url'
];

$valid=Validx::validate($_POST, $rules);

if($valid->hasErrors()){
  echo '';

}else{
  echo '';
}

Validx use session to retrive error messages from any where your project. So its better to session_start first. After complete show error message please put the code end of your file Validx::clearMessages()

Validation Rules Setup

Its much important to setup validations rule. All validation rules are passing throw as an array. Here the syntax

 
$rules=array(
    'input_field_name1'=>'rule1||rule2||...||ruleN' ,
    .
    .
    .
    'input_field_nameN'=>'rule1||rule2||...||ruleN' 
  );

array key is for input field name and array value is use for rule/s.

Validating Inputs or data

After complete setup rules then you should to execute validation.

 
$valid=new Validx;

$form=$valid->validate($inputs, $rules);

The validate() method is check all inputs with your rules. Here input is an array. If you want to form validation then just put $_POST/$_GET otherwise put your desire values. After complete validating the method return an object.

Checking Validation Status

Its so important to know what are happen after complete the validation. You may know it by using Validx::hasErrors()

 
if($form->hasErrors()){
  //do something
}else{
  //do something else
}

This method return true if there are any error/s occured otherwise return false

Fetching All Errors

Now we fetch all error which are occured after validation. To getting all errors you have to use Validx::allErrors() method.

 
if($form->hasErrors()){
  echo '
    '; foreach($form->allErrors() as $err){ echo '
  • '.$err.'
  • '; } echo '
' }
This method return an array as message. So you can easily fetch all error messages.

Fetching Specific Input Messages

Now we fetch all error for specific input which are occured after validation. To getting all errors you have to use Validx::getErrors('input_field_name') method.

 
if($form->hasErrors()){
  echo '
    '; foreach($form->getErrors('email') as $err){ echo '
  • '.$err.'
  • '; } echo '
' }
This method return an array as message. So you can easily fetch all error messages.

Set Custom Error Message

In Validx you can set error message as you wish. The message will be an array. Basically its a multidimentional array.

 
$message=array(
  'input_name_1'=>array(
    'rule_name_1'=>'message',
    .
    .
    'rule_name_N'=>'message'
  ),
    .
    .
  'input_name_N'=>array(
    'rule_name_1'=>'message',
    .
    .
    'rule_name_N'=>'message'
  )
);

See an example
 
$input=array(
  'name'=>'Nahid Bin Azhar',
  'email'=>'info@nahid.co'
);

$rule=array(
  'name'=>'required',
  'email'=>'required||email'
);

$message=array(
  'name'=>array(
    'required'=>'Please enter your name'
  ),
  'email'=>array(
    'required'=>'Please enter your email',
    'email'=>'This is not valid email'
  )
);

$valid=Validx::validate($input, $rule, $message);


Actually Validx::validate() method has 3 arguments. Last one is for messages which is optional.

List of Rules

Here is list of all rules. Basically every rules is a function. So we follow a declaration convension for rules. Rules may have its agruments. arguments and functions are sperated by ':' colon and multiple arguments are seperated by ',' comma.

Rule Descriptions
required The field under required validation must present a value
min:value The filed under min validation must have minimum value range. The rule have one argument where you specify minmum value
max:value The field under min validation must have maximum value range. The rule have one argument where you specify maximum value
file This rules confirm that the input is a file.
date The field under date validation must have a date format.
date_max:date The field under date_max validation must maximum from argument.
date_min:date The field under date_min validation must min from argument.
in:val,val1,... The field under in validation must be contain in the given list.
not_in:val,val1,... This rules is reverse from in validation. The field under in validation must not contain in the given list.
image The field under image validation rule must have an image file.
file_type:jpg,mp3,foo,bar,... The field under file_type validation rule must have an file and off course the file type included the given list.
same:field The field under same validation rule must same value as given field.
different:field The field under different validation rule must different from given field.
email The field under email validation rule must have an email.
url The field under url validation rule must have an url.
ip The field under ip validation rule must have an IP.
numeric The field under numeric validation rule must have numeric value.
integer The field under integer validation rule must have an integer value.
range:min,max The field under range validation rule must have value between min and max.
pattern:regular_expression The field under pattern validation rule must follow by the given regex.
unique:table,column The field under validation must be unique on a given database table. For execute the process you have to passes connection string as an argument during class initializing. ex: $valid=new Validx($connection);. We use MySQLi.
exist:table,column The field under validation must exist on a given database table. For execute the process you have to passes connection string as an argument during class initializing. ex: $valid=new Validx($connection);. We use MySQLi.

Thank you