Ver Mensaje Individual
  #8 (permalink)  
Antiguo 30/07/2011, 01:38
GrupoC
 
Fecha de Ingreso: noviembre-2009
Mensajes: 226
Antigüedad: 15 años
Puntos: 19
Respuesta: [aporte] sistema de validación de datos

Código PHP:
    private function validateSTRING($value)
    {
        
$return="El campo $this->field ";
        if(isset(
$this->minchr) && strlen($value)<$this->minchr)
            return 
$this->showValidation($return." debe tener como m&iacute;nimo $this->minchr caracteres");
        if(isset(
$this->maxchr) && strlen($value)>$this->maxchr)
            return 
$this->showValidation($return." debe tener como m&aacute;ximo $this->maxchr caracteres");
        if(
$this->accnum==&& preg_match('`[0-9]`',$value))
            return 
$this->showValidation($return." no puede contener n&uacute;meros");
        if(
$this->accnum==&& preg_match('`[0-9]`',$value))
            return 
$this->showValidation($return." no puede contener n&uacute;meros");
        if(
$this->accsp==&& strpos($value," ")!==false)
            return 
$this->showValidation($return." no puede contener espacios");

        if(isset(
$this->like)) {
            
$last=(substr($this->like,0,1)=="%") ? true false;
            
$first=(substr($this->like,-1)=="%") ? true false;

            
$str=str_replace("%","",$this->like);
            
            if(
$first && $last) {
                if(
strpos(strtolower($value),strtolower($str))===false)
                    return 
$this->showValidation($return." debe contener la cadena '$str'");
            } elseif(
$first) {
                if(
strtolower(substr($value,0,strlen($str)))!=strtolower($str))
                    return 
$this->showValidation($return." debe comenzar por '$str'");
            } elseif(
$last) {
                if(
strtolower(substr($value,-strlen($str)))!=strtolower($str))
                    return 
$this->showValidation($return." debe finalizar en '$str'");
            }
        }
        return 
$this->showValidation();
    }

    private function 
formatSize($size) {
        
$sizes = array(" Bytes"" KB"" MB"" GB"" TB"" PB"" EB"" ZB"" YB");
        return (
round($size/pow(1024,($i=floor(log($size1024)))),2).$sizes[$i]);
    }
    
    private function 
validateFILE($value,$row)
    {
        
$return="El campo $this->field ";
        
$size=$this->elements[$row]["value"]["size"];
        
$type=$this->elements[$row]["value"]["type"];

        
$minsize=(isset($this->minsize)) ? true false;
        
$maxsize=(isset($this->maxsize)) ? true false;

        if(
$minsize && $maxsize)
            if(
$size>$this->maxsize || $size<$this->minsize)
                return 
$this->showValidation($return." debe tener un tama&ntilde;o entre [".$this->formatSize($this->minsize)." , ".$this->formatSize($this->maxsize)."]");
        if(
$minsize && !$maxsize)
            if(
$size<$this->minsize)
                return 
$this->showValidation($return." debe tener un tama&ntilde;o mayor que ".$this->formatSize($this->minsize));
        if(
$maxsize && !$minsize)
            if(
$size>$this->maxsize)
                return 
$this->showValidation($return." debe tener un tama&ntilde;o menor que ".$this->formatSize($this->maxsize));
        
        if(isset(
$this->accformats)) {
            if(
array_search($type$this->accformats)===false) {
                
$string="[ ";
                for(
$i=0;$i<count($this->accformats);$i++) {
                    
$elements=explode("/",$this->accformats[$i]);
                    
$string.=$elements[1]." , ";
                }
                
$string=substr($string,0,-2)."]";
                return 
$this->showValidation($return." debe tener formato $string");
            }
        }
        
        return 
$this->showValidation();
    }
    
    private function 
unsetProperties()
    {
        
$this->accnum="0";
        
$this->accsp="0";
        
$this->sql="0";
        unset(
$this->field);
        unset(
$this->min);
        unset(
$this->max);
        unset(
$this->type);
        unset(
$this->min);
        unset(
$this->max);
        unset(
$this->numdec);
        unset(
$this->sep);
        unset(
$this->only);
        unset(
$this->minchr);
        unset(
$this->maxchr);
        unset(
$this->tfh);
        unset(
$this->like);
        unset(
$this->minsize);
        unset(
$this->maxsize);
        unset(
$this->accformats);
        unset(
$this->mindate);
        unset(
$this->maxdate);
    }
    public function 
validateElements()
    {
        for(
$i=0;$i<$this->count;$i++)
        {
            
$element=$this->elements[$i];
            
$this->field=$element["name"];
            
$this->valtype= isset($element["type"]) ? $element["type"] : "";
            
$force=$element["force"];
            
            if(isset(
$element["valid"]))
            {
                if(
array_search($element["value"],$element["valid"])===false)
                    
$this->errors_in_validation.=$this->showValidation("El campo $this->field no es correcto");
                else
                    
$this->errors_in_validation.=$this->showValidation();
                continue;
            }
            
            if(!
is_array($element["value"]) && $this->valtype!="file")
                
$value=$element["value"];
            else
                
$value=$element["value"]["name"];
            
            if(
$force==&& empty($value)) {
                
$this->errors_in_validation.=$this->showValidation();
                continue;
            }
            if(
$force==&& empty($value)) {
                
$this->errors_in_validation.=$this->showValidation("El campo $this->field es obligado");
                continue;
            }
            
            foreach(
$element as $attr => $propertie)
                
$this->$attr=$element[$attr];
            
            
$function=strtoupper($this->valtype);
            
$function="validate".$function;
            if(
$function=="validateFILE")
                
$this->errors_in_validation.=$this->$function($element["value"],$i);
            else
                
$this->errors_in_validation.=$this->$function($element["value"]);
            
            
$this->unsetProperties();
        }
        return 
substr($this->errors_in_validation,0,-1);
    }
}

?> 
Y esta es la última parte del code.

Espero que sirva de algo!
__________________
Mi blog: magdkudama.com

Mi clase de validación de datos: magdkudama.com/validation