Ver Mensaje Individual
  #23 (permalink)  
Antiguo 03/01/2008, 13:15
Avatar de jerkan
jerkan
 
Fecha de Ingreso: septiembre-2005
Mensajes: 1.607
Antigüedad: 18 años, 8 meses
Puntos: 19
Re: Clase Formulario

Al final, opté por clases abstractas para los validadores:
Código PHP:
    // clase base CValidator
    
        
abstract class CValidator
        
{
        
// atributos
        
            
protected $field;            // campo de formulario
            
protected $error_msg;        // error message
        
        // métodos
        
            
abstract public function validate();
            
            public function 
setErrorMsg($error_msg)        {    $this->error_msg $error_msg;    }

            public function 
getErrorMsg()    {    return $this->error_msg;     }
                    
        } 
// end clase base CValidator
        
        
    // class CValidatorNotEmpty
    
        
class CValidatorNotEmpty extends CValidator 
        
{
            
            public function 
__construct(CFormField &$oFormField)
            {
                
$this->field $oFormField;
                
$this->error_msg sprintfERROR_VALIDATOR_EMPTY$oFormField->getLabel() );
            }
            
            
            public function 
validate()    
            {    
                
$value $this->field->getValue();
                
                return !empty(
$value);    
            }
            
        } 
// end class CValidatorNotEmpty
        
            
    // class CValidatorEmail
    
        
class CValidatorEmail extends CValidator 
        
{
            
            public function 
__construct(CFormField &$oFormField)
            {
                
$this->field $oFormField;
                
$this->error_msg sprintfERROR_VALIDATOR_EMAIL$oFormField->getLabel() );
            }
            
            public function 
validate()    
            {    
                
$value $this->field->getValue();
                
                return empty(
$value) || is_email($value);    
            }
                        
        } 
// end class CValidatorEmail 
Cada validador tiene su constructor particular (número de campos variable) y su mensaje de error propio (se puede cambiar con el método setErrorMsg).