Ver Mensaje Individual
  #1 (permalink)  
Antiguo 16/01/2009, 17:02
Avatar de Italico76
Italico76
 
Fecha de Ingreso: abril-2007
Mensajes: 3.303
Antigüedad: 17 años, 1 mes
Puntos: 292
Sonrisa Clase formulario

Bueno amigos....... hice una clase formulario que creo es bastante versatil...... permite incorporar CSS a cualquier elemento........ y tiene bastantes opciones.

Aca una demo

La clase es funcional para formularios simples (80% de ellos) pero habria que terminar el tema del <LABEL> donde me falto cubrir el caso de que se quieran meter controles dentro de <LABEL> ... </LABEL>

Puede que falte algun control........de hecho el SELECT OPTION para los combo lo deje ..... a ver si lo termino.

Aca el codigo:

Código PHP:
<?php
class Form {  // clase para hacer formularios
  
private $_action;   // = "< ?= $_SERVER['PHP_SELF'] ? >"
  
private $_method
  private 
$_enctype
  private 
$_elementos = array();
  private 
$_cant =-1// elemento actual
  
private $_css = array (); // css aplicado a todos los elementos (se usa al inicio cuando no hay elementos)
  
private $_cssElem = array ();  // del elemento con el que se esta trabajando actualmente
  
  // los atributos debe usar COMILLAS DOBLES!
  // usar http://www.php.net/add_slashes  par las comillas
  
  
public function __construct($action,$method,$enctype="application/x-www-form-urlencoded"){ 
    
$this->_action $action;  // archivo q procesa el formulario
    
$this->_method $enctype// "GET" o "POST"
    
$this->_enctype $enctype;   
  }
     
  
// SETTERS ------------------------->
      
  
public function addText($name,$value=''){    
    if (!empty(
$value)) $value="value='$value'";    
    
$this->_elementos[] = array('type'=>'text','name'=>$name'value'=>$value);            
    
$this->_cant++;
  }
  
  public function 
addTextArea($name,$value=''){         
    
$this->_elementos[] = array('type'=>'textarea','name'=>$name'value'=>$value
    
'rows'=>'10''cols'=>'30');                
    
$this->_cant++;
  }
      
   public function 
addPassword($name,$value=''){     
    if (!empty(
$value)) $value="value='$value'";    
    
$this->_elementos[] = array('type'=>'password','name'=>$name'value'=>$value);        
    
$this->_cant++;
  }   
  
  public function 
addCheckbox($name,$value=''){     
    if (!empty(
$value)) $value="value='$value'";    
    
$this->_elementos[] = array('type'=>'checkbox','name'=>$name'value'=>$value);        
    
$this->_cant++;
  }   
  
  public function 
addRadio($name,$value=''){     
    if (!empty(
$value)) $value="value='$value'";    
    
$this->_elementos[] = array('type'=>'radio','name'=>$name'value'=>$value);        
    
$this->_cant++;
  }   
  
  public function 
addSubmit($name,$value){     
    if (!empty(
$value)) $value="value='$value'";    
    
$this->_elementos[] = array('type'=>'submit','name'=>$name'value'=>$value);        
    
$this->_cant++;
  }   
  
  public function 
setTitle($title){  // rotulo -no admite css-
     
$this->_elementos[$this->_cant]['title']=$title;     
  }    
   
  public function 
setAction ($action){  
     
$this->_action $action;    
  }
   
  public function 
setValue($value){
    
// Si $value contiene '< ?'  entonces  debe luego EJECUTARSE ese codigo (usar eval()
    
if ($elemento['type']!='textarea'$value="value='$value'";    
    if (!empty(
$value)) $this->_elementos[$this->_cant]['value']=$value;
  }

   public function 
setId($id){ 
     
$this->_elementos[$this->_cant]['id']="id='$id'";     
   }    
          
   
// <LABEL FOR  .... la posibilidad de encerrar controles dentro de Label no fue implementada...........aun
   
   
public function setLabel($label){
     
$this->_elementos[$this->_cant]['label']="<label>$label</label>";     
   }
   public function 
setLabelFor($label,$id=NULL){
    if(
$id==NULL$id $this->_elementos[$this->_cant]['id'];
    
$this->_elementos[$this->_cant]['label']="<label for $id>{$label}</label>";     
   }

  public function 
setTabIndex($value){  // TabIndex    
    
if ($this->_intOK($value)) $this->_elementos[$this->_cant]['tabindex']="tabindex='$value'";  
  }    
  
  public function 
setRows($rows){
    if (
$this->_elementos[$this->_cant]['type']=='textarea')  $this->_elementos[$this->_cant]['rows']=$rows;
  }    
  public function 
setCols($cols){
    if (
$this->_elementos[$this->_cant]['type']=='textarea')  $this->_elementos[$this->_cant]['cols']=$cols;
  }    

  public function 
setChecked(){      
    
// ver que el elemento sea chequeable (checbox, combo,... radio)
    
$this->_elementos[$this->_cant]['checked']="checked='checked'";     
  }
  public function 
unsetChecked(){  // unCheck
    
unset($this->_elementos[$this->_cant]['checked']);  
  }    
     
  public function 
setDisabled(){
    
$this->_elementos[$this->_cant]['disabled']="disabled='True'";  
  }    
  public function 
unsetDisabled(){
    unset(
$this->_elementos[$this->_cant]['disabled']);  
  }    
  
 public function 
setReadOnly(){     
    
$this->_elementos[$this->_cant]['readonly']="readonly='True'";  
  }      
 public function 
unsetReadOnly(){     
    unset(
$this->_elementos[$this->_cant]['readonly']); 
  }     
  

  public function 
addClass($class){ //ok
     
if ($this->_cant==-1) {
       
$this->_soloUnCss ('class',$class);    
     }else{        
        unset (
$this->_elementos[$this->_cant]['css']);      
        
$this->_elementos[$this->_cant]['css'] = array('css_tipo'=>'class','value'=>$class);        
     }      
   }  
    
  public function 
addStyle($style){
    if (
$this->_cant==-1) {
      
$this->_soloUnCss ('stlye',$style);    
    }else{
        unset (
$this->_elementos[$this->_cant]['css']);      
        
$this->_elementos[$this->_cant]['css'] = array('css_tipo'=>'style','value'=>$style);        
        
//echo $this->_elementos[$this->_cant]['css']['css_tipo'];
    
}      
  }       
    
  public function 
addCode ($php){    // carga en ELEMENTOS[] ['php']  un fragmento de PHP que luego sera ejecutado   
  
}
  
  
// METODOS PRIVADOS          ------------------------------------------>

  
private function _intOK($val){  // devuelve si es un numero y es entero (php.net)
    
return ($val !== true) && ((string)(int) $val) === ((string) $val);
  }

  
  private function 
_soloUnCss ($tipo,$value){  // o es STYLE o es CLASS pero no ambos
    
unset ($this->_css);
    
$this->_css = array('css_tipo'=>$tipo,'value'=>$value); 
    
//echo $this->_css[$tipo];
  
}
  
    private function 
_allElems(){  // debe ser private    
    
$all='';      
      foreach (
$this->_elementos as $elemento){            
        
         if (
array_key_exists('css',$elemento)){
           
$css "{$elemento['css']['css_tipo']}='{$elemento['css']['value']}'"
         }else{        
           
$css "{$this->_css['css_tipo']}='{$this->_css['value']}'";
         }  
         
         if (
array_key_exists('label',$elemento)){
           
$rotulo $elemento['label'];
         }else{
           
$rotulo $elemento['title'];
         }          
        
        switch (
$elemento['type']){           
          case 
checkbox:
          
$all .= "<input type='{$elemento['type']}' name='{$elemento['name']}' {$elemento['value']} {$elemento['checked']} {$elemento['id']} {$elemento['tabindex']} {$elemento['disabled']} {$elemento['readonly']} />{$elemento['title']} <br>";       
          break;
          
          case 
radio:
          
$all .= "<input type='{$elemento['type']}' name='{$elemento['name']}' {$elemento['value']} {$elemento['checked']} {$elemento['id']} {$elemento['tabindex']} {$elemento['disabled']} {$elemento['readonly']} />{$elemento['title']} <br>";       
          break;

          case 
textarea// http://htmlhelp.com/reference/html40/forms/textarea.html
          
$all .= "{$rotulo}<br/> <textarea name='{$elemento['name']}' rows='{$elemento['rows']}' cols='{$elemento['cols']}' {$elemento['id']} {$elemento['tabindex']} {$elemento['disabled']} {$elemento['readonly']} $css >{$elemento['value']}</textarea><br/>";    
          break;
                  
          default:
          
$all .= "{$rotulo}<br/> <input type='{$elemento['type']}' name='{$elemento['name']}' {$elemento['value']} {$elemento['id']} {$elemento['tabindex']} {$elemento['disabled']} {$elemento['readonly']} $css/><br/>";    
          break;          
        }             
        
      }
     return 
$all;
  }

  
// GETTERS          ------------------------------------------------------------->
  
 
public function getValue(){           
   return 
$this->_elementos[$this->_cant]['value'];
 }  
 
 public function 
display(){  // aca devuelvo el formulario para imprimirlo  (usar metodos magicos sino)
   
$elems $this->_allElems();
   
// podria ser necesario reemplazar todas las comillas simples x dobles ...........W3C
   
$formu "<form  action='$this->_action'  method='$this->_method'  ENCTYPE='$this->_enctype'>$elems\n\r</form>";
   return  
$formu;
 }      
    
}  
?>
EDIT: fue editada con las primeras correcciones de Enrique
__________________
Salu2!

Última edición por Italico76; 17/01/2009 a las 06:27