Pogledajte određenu poruku
Staro 27. 01. 2013.   #13
sinisake
član
Certified
 
Datum učlanjenja: 25.07.2008
Poruke: 76
Hvala: 15
296 "Hvala" u 10 poruka
sinisake is on a distinguished roadsinisake is on a distinguished roadsinisake is on a distinguished roadsinisake is on a distinguished road
Default

PHP kôd:
<?php

class Validator
{
    
    var 
$errors=array();
    var 
$output=array();
    


  public function 
process($vars)
  {
      
     for(
$i=0;$i<count($vars);$i++) 
    
    {
        
        
$title=$vars[$i][0];
        
$rules=$vars[$i][1];
        
$var=$vars[$i][2]; 
        
        
    
//check required field
    
if(strpos($rules'req') !== false
    {
        
    if(
$this->required($var))

        
     
    {
     
$this->errors[] = $title." is required field!";    
        
    }
        
        
    }
    
        
//only text is allowed
            
            
if(strpos($rules,'text')!==FALSE
            
            {
                if(
$this->only_text($var)) 
                {
                
$this->errors[]= $title' field should contain only letters!<br />';      
                    
                }
                
            }
    
    
    
    
      
//check minimal number of chars
    
if (strpos($rules'min=') !== false)
    {
      
$num $this->min($var$rules);
      if (
is_numeric($num))
      {
        
$this->errors[] = $title." should have at least $num characters!";
      } elseif (
$num === false)
      {
        
$this->errors[] = 'Invalid rule.';
      }
    }
    
    
//check max number of chars
     
if (strpos($rules'max=') !== false)
    {
      
$num $this->max($var$rules);
      if (
is_numeric($num))
      {
        
$this->errors[] = $title." should have at most $num characters!";
      } elseif (
$num === false)
      {
        
$this->errors[] = 'Invalid rule.';
      }
    }
    
    
//check email
    
    
if(strpos($rules,'valid_email')!==FALSE
            
            {
            
if(
$this->valid_email($var))
  {
  
$this->errors[]= $title' is not a valid email address!<br />';
  }

                
            }
    
if(!
is_array($var)) {    
        
$this->output[]=$title' : '$var'<br />';    
        }
        else 
        {
            
$this->output[]=$title' : 'implode(',',$var). '<br />';
        }    
  }
  
  
  
            
  
  
  if(!empty(
$this->errors)) 
    
    {
    return 
false;    
        
    }
    
    else 
    {
    return 
true;    
    }
    
  
  
  }
  
  
//required field
  
private function required($var)  
  {
      
    
                if(empty(
$var)) 
                {
                return 
true;    
                    
                }
            
                
              
      
      
      
  }
  
//only text
  
  
private function only_text($var
  {
    if(
preg_match('%[^A-Za-z ]%',$var))
    return 
true;
  
            
      
  }
  
  
//valid email
  
  
private function valid_email($var)  
  {
    if(!
filter_var($varFILTER_VALIDATE_EMAIL))
  {
 
return 
true;
  }  
      
      
      
  }
  
  
//min number of chars
  
private function min($var$rules)
  {
    
preg_match('%min=([0-9]{1,10})%'$rules$match);
    if (empty(
$match[1]))
      return 
false;
    return 
strlen($var) < (int)$match[1] ? $match[1] : true;
  }
  
//max number of chars
  
  
private function max($var$rules
  {
 
preg_match('%max=([0-9]{1,10})%'$rules$match);
    if (empty(
$match[1]))
      return 
false;
    return 
strlen($var) > (int)$match[1] ? $match[1] : true;      
      
  }
  
  

}
//$validator = (new Validator($vars))->process();  
?>
E, samo da pitam, pre nego sto nastavim (ili odustanem) ovako bi to trebalo, ili da se ne zakopavam dalje?
sinisake je offline   Odgovorite uz citat