PHP Classes

PHP Validation Class: Validate form inputs according to different rules

Recommend this page to a friend!
  Info   View files Example   View files View files (7)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 183 All time: 8,660 This week: 107Up
Version License PHP version Categories
php-form-validator 1.0.0Free For Educatio...5PHP 5, Validation
Description 

Author

This class can validate form inputs according to different rules.

It provides different functions that can validate values using different rules and returns 1 or 0 depending on whether the value is valid.

Currently it can validate required values, email addresses, minimum, maximum and exact length, number between a range, letters or digits, check file name extensions, IP addresses, date ranges, etc..

Picture of raghavender
  Performance   Level  
Name: raghavender <contact>
Classes: 2 packages by
Country: India India
Age: 36
All time rank: 3660253 in India India
Week rank: 91 Up11 in India India Up

Example

<?php
require_once 'class.validator.php';
require_once
realpath(__DIR__.'/../class.rules.php');
 
function
validate($funName,$formDataArray){
   
$formRulesObj = new FormRules();
   
$validator = new Validations();
   
$loginRules = $formRulesObj->$funName();
   
$response = $status = '';
    foreach(
$loginRules['rules'] as $name=>$conds){
        foreach(
$conds as $cond=>$value){
           
$valueOptions = array();
            if(
is_array($value)){
               
$valueOptions = $value;
                if(
$valueOptions[0] === true){
                   if(
is_array($valueOptions[1])){
// print_r($valueOptions[1]);
                       
$options = array('condition'=>$valueOptions[1]);
                        if(isset(
$formDataArray[$name])) $status = $validator->$cond($formDataArray[$name],$options);
                        if(
$status == 0){
                           
$response[$name][$cond] = $loginRules['messages'][$name][$cond];
                        }
                   } else {
                       
$status = $valueOptions[1]($formDataArray[$name]);
                        if(
$status == 0){
                            
$response[$name][$cond] = $loginRules['messages'][$name][$cond];
                        }
                   }
                }
            } else if(
$value === true || $value != ''){
               
$options = array('condition'=>$value);
                if(isset(
$formDataArray[$name])) $status = $validator->$cond($formDataArray[$name],$options);
                if(
$status == 0){
                   
$response[$name][$cond] = $loginRules['messages'][$name][$cond];
                }
            }
        }
    }
    if(!empty(
$response)){
       
// Set only one error message for each input field
       
foreach($response as $k=>$resp){
            if(
is_array($resp) && count($resp) > 1){
               
$new_res[$k][key($resp)] = $a = array_shift($resp);
            } else {
               
$new_res[$k] = $resp;
            }
        }
        return
$errorMsgs = json_encode($new_res);
    } else {
        return
true;
    }
   
}
?>


Details

This class validates form at server side with rules and messages by user. It works as Jquery validation plugin fashion. When click on form submit button, will make a ajax call and send form data. After validate the data with defined rules, display the error messages to corresponding input fields which are defined as messages.

Features: - Server side validation, but looks like client side validation - Easy to define rules and messages for each input field - Rules and error messages are define in array format - It Validates all input types including input type 'file' - Validation Types: not_empty, min_length, max_length, alphaonly, alphanumonly, splalphanumonly, ip address,regular expressions, file extensions, date_format, min_date, max_date


  Files folder image Files  
File Role Description
Files folder imagelibs (2 files)
Accessible without login Plain text file ajax_call.php Aux. Request form submit data
Plain text file class.rules.php Class Define the rules and messages
Accessible without login Plain text file index.php Aux. Bootstrap form with input fileds
Accessible without login Plain text file jquery.min.js Data Jquery default library file
Accessible without login Plain text file readme.md Doc. describes class features

  Files folder image Files  /  libs  
File Role Description
  Plain text file class.validator.php Class class of validation functions
  Accessible without login Plain text file validate.php Example validate form input fields

 Version Control Unique User Downloads Download Rankings  
 100%
Total:183
This week:0
All time:8,660
This week:107Up