Code snippets for symfony 1.x

Navigation

Refine Tags

Snippets tagged "validator 12"

CPF e CNPJ Validator

<?php
class sfValidatorCpfCnpj extends sfValidatorBase {
 
  /**
   * Configures the current validator
   *
   * Available options (required):
   *
   *  * tipo: CPF or CNPJ
   *
   * @param array $options   An array of options
   * @param array $messages  An array of error messages
   *
   * @see sfValidatorBase
   **/
 
  protected function configure($options = array(), $messages = array())
  {
    $this->addRequiredOption('tipo');
    $this->setMessage('invalid', '%tipo% %value% não é válido.');
  }
 
  protected function validaCPF($cpf) {
        $cpf = str_replace(array('.','-'),'',$cpf);
 
        $soma = 0;
 
        if (strlen($cpf) <> 11)
        return false;
 
        // Verifica 1º digito
        for ($i = 0; $i < 9; $i++):
            $soma += (($i+1) * $cpf[$i]);
        endfor;
 
        $d1 = ($soma % 11);
 
        if ($d1 == 10):
            $d1 = 0;
        endif;
 
        $soma = 0;
 
        // Verifica 2º digito
        for ($i = 9, $j = 0; $i > 0; $i--, $j++):
            $soma += ($i * $cpf[$j]);
        endfor;
 
        $d2 = ($soma % 11);
 
        if ($d2 == 10):
            $d2 = 0;
        endif;
 
        if ($d1 == $cpf[9] && $d2 == $cpf[10]):
            return true;
        else:
            return false;
        endif;
    }
 
    protected function validaCNPJ($cnpj) {
 
        $cnpj = str_replace(array('.','/','-'),'',$cnpj);
 
        if (strlen($cnpj) <> 14)
        return false;
 
        $soma = 0;
 
        $soma += ($cnpj[0] * 5);
        $soma += ($cnpj[1] * 4);
        $soma += ($cnpj[2] * 3);
        $soma += ($cnpj[3] * 2);
        $soma += ($cnpj[4] * 9);
        $soma += ($cnpj[5] * 8);
        $soma += ($cnpj[6] * 7);
        $soma += ($cnpj[7] * 6);
        $soma += ($cnpj[8] * 5);
        $soma += ($cnpj[9] * 4);
        $soma += ($cnpj[10] * 3);
        $soma += ($cnpj[11] * 2);
 
        $d1 = $soma % 11;
        $d1 = $d1 < 2 ? 0 : 11 - $d1;
 
        $soma = 0;
        $soma += ($cnpj[0] * 6);
        $soma += ($cnpj[1] * 5);
        $soma += ($cnpj[2] * 4);
        $soma += ($cnpj[3] * 3);
        $soma += ($cnpj[4] * 2);
        $soma += ($cnpj[5] * 9);
        $soma += ($cnpj[6] * 8);
        $soma += ($cnpj[7] * 7);
        $soma += ($cnpj[8] * 6);
        $soma += ($cnpj[9] * 5);
        $soma += ($cnpj[10] * 4);
        $soma += ($cnpj[11] * 3);
        $soma += ($cnpj[12] * 2);
 
 
        $d2 = $soma % 11;
        $d2 = $d2 < 2 ? 0 : 11 - $d2;
 
        if ($cnpj[12] == $d1 && $cnpj[13] == $d2):
            return true;
        else:
            return false;
        endif;
    }
 
  protected function doClean($value)
  {
    $tipo = $this->getOption('tipo');
 
    if($tipo == 'CPF'):
            if(!$this->validaCPF($value)):
                throw new sfValidatorError($this, 'invalid', array('tipo' => $this->getOption('tipo'), 'value' => $value));
                return false;
            endif;
    elseif($tipo == 'CNPJ'):
            if(!$this->validaCNPJ($value)):
                throw new sfValidatorError($this, 'invalid', array('tipo' => $this->getOption('tipo'), 'value' => $value));
                return false;
            endif;
    else:
        throw new sfValidatorError($this, 'invalid', array('tipo' => $this->getOption('tipo'), 'value' => $value));
        return false;
    endif;
 
    return $value;
  }  
}
 
?>
 
by Anderson da Veiga on 2010-02-27, tagged 12  13  14  cnpj  cpf  symfony  validator 

sfValidatorFileImage : a simple validator for image file (symfony 1.2)

Description :

This is a class for symfony 1.2 for images. It extends the class sfValidatorFile.

<?php
 
/**
 * sfValidatorFileImage
 *
 * Original validator for symfony 1.0 : http://snippets.symfony-project.org/snippet/259
 *
 * @package    symfony
 * @subpackage validator
 * @author     Yoann Brieux <yoann |dot¤ brieux #at] gmail ~dot} com>
 * @version    0.1
 */
class sfValidatorFileImage extends sfValidatorFile
{
  /**
   * @param array $options   An array of options
   * @param array $messages  An array of error messages
   *
   * @see sfValidatorFile
   */
  protected function configure($options = array(), $messages = array())
  {
    parent::configure($options, $messages);
 
    $this->addMessage('invalid_image', '%value% is an incorrect image file.');
    $this->addMessage('max_height', '"%value%" height is too long (%max_height% pixels max).');
    $this->addMessage('min_height', '"%value%" height is too short (%min_height% pixels min).');
    $this->addMessage('max_width', '"%value%" width is too long (%max_width% pixels max).');
    $this->addMessage('min_width', '"%value%" width is too short (%min_width% pixels min).');
 
    $this->addOption('max_height');
    $this->addOption('min_height');
    $this->addOption('max_width');
    $this->addOption('min_width');    
    $this->addOption('is_only_image',false);    
  }
 
  /**
   * @see sfValidatorFile
   */
  protected function doClean($value)
  {
    $clean = parent::doClean($value);
 
    $size = @getimagesize($clean->getTempName());
 
    if (!$size && !$this->getOption('is_only_image')) 
    {
        return $clean;
    }
 
    if (!$size){    
      throw new sfValidatorError($this, 'invalid_image', array('value' => $value['name']));
    }
 
    list($width, $height) = $size;
 
    if($this->getOption('max_height') < $height){
        throw new sfValidatorError($this, 'max_height', array('value' => $value['name'], 'max_height' => $this->getOption('max_height')));
    }
 
    if($this->getOption('min_height') > $height){
        throw new sfValidatorError($this, 'min_height', array('value' => $value['name'], 'min_height' => $this->getOption('min_height')));
    }
 
    if($this->getOption('max_width') < $width){
        throw new sfValidatorError($this, 'max_width', array('value' => $value['name'], 'max_width' => $this->getOption('max_width')));
    }
 
    if($this->getOption('min_width') > $width){
        throw new sfValidatorError($this, 'min_width', array('value' => $value['name'], 'min_width' => $this->getOption('min_width')));
    }
 
    return $clean;
  }
}
 

How to use it?

In the your form library :

    $this->validatorSchema['field'] = new sfValidatorFileImage(array(
        'required'                          =>  false,
        'path'                              =>  sfConfig::get('sf_upload_dir').'/news/',
        'max_height'                        =>  238,
        'min_height'                        =>  238,
        'max_width'                         =>  270,
        'min_width'                         =>  270,
        'mime_types'                        =>  array('image/jpeg','image/pjpeg','image/png','image/x-png','image/gif','application/x-shockwave-flash')
        )
    );
 

If the variable "is_only_image" is true and function "getimagesize" return false (is not a valid image for example), this validator cause an sfValidatorError exception with the message "[your file] is an incorrect image file."

by Yoann Brieux on 2009-06-15, tagged 12  file  files  image  images  symfony  validator  validators