PHP Classes

File: libs/validate.php

Recommend this page to a friend!
  Classes of raghavender  >  PHP Validation Class  >  libs/validate.php  >  Download  
File: libs/validate.php
Role: Example script
Content type: text/plain
Description: validate form input fields
Class: PHP Validation Class
Validate form inputs according to different rules
Author: By
Last change:
Date: 6 years ago
Size: 2,122 bytes
 

Contents

Class file image Download
<?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;
    }
   
}
?>

For more information send a message to info at phpclasses dot org.