Snippets

Create an account or login to be able to add, comment and rate snippets.

Navigation

Refine Tags

Snippets tagged "validation" Snippets tagged "validation"

Age Validation

Say you wanted to validate that someone was over 18 years of age, its acutally quite simple once you know what your doing, but it took me a while to work out all the bits, so i thought i would put it in here to save some one a bit of time...

on the template you have

<p>
 
  <label for="date_of_birth">Date of Birth:</label>
  <?php echo form_error('day');  ?>
  <?php echo form_error('month');  ?>
  <?php echo form_error('year');  ?>
  <?php echo select_day_tag('day', $sf_params->get('day')) ?>
 
  <?php echo select_month_tag('month', $sf_params->get('month')) ?>
  <?php echo select_year_tag('year', $sf_params->get('year'),array('year_start'=>'1900','year_end'=>date("Y"))) ?>
</p>
 

in signup.yml (due to the way i built my form, you dont actually need to validate the day and month, since there is no way to leave them empty, but i have put the code in for completness)

methods:
  post: [day, month, year, gender]
 
names:
  day:
    required:     true
    required_msg: You must verify your age.
 
  month:
    required:     true
    required_msg: You must verify your age.
 
  year:
    required:     true
    required_msg: You must verify your age.
    validators:   ageValidator
 
 
ageValidator:
    class:         myAgeValidator
    param:
      too_young_error: You must be over 18 to enter.
 

now in /lib/AgeValidator.class.php, we put the code to check the dates.

class myAgeValidator extends sfValidator
{
  public function initialize($context, $parameters = null)
  {
    // initialize parent
    parent::initialize($context);
 
    // set defaults
    $this->setParameter('too_young_error', 'Invalid input');
    $this->getParameterHolder()->add($parameters);
 
    return true;
  }
 
  public function execute(&$value, &$error)
  {
 
// get the passed values for the date fields
    $day_param = sfContext::getInstance()->getRequest()->getParameter('day');
    $month_param = sfContext::getInstance()->getRequest()->getParameter('month');
    $year_param = sfContext::getInstance()->getRequest()->getParameter('year');
 
    $min_age=strtotime("-18 YEAR");
    $entrant_age= strtotime( $year_param . "-" . $month_param . "-" . $day_param);
 
# just in case ;-)
#sfContext::getInstance()->getLogger()->debug('min_age->'.$min_age);
#sfContext::getInstance()->getLogger()->debug('entrant_age->'.$entrant_age);
#sfContext::getInstance()->getLogger()->debug('param-day->'.$day_param);
#sfContext::getInstance()->getLogger()->debug('param-month->'.$month_param);
#sfContext::getInstance()->getLogger()->debug('param-year->'.$year_param);
 
if ($entrant_age <$min_age ){
    return true;
}
 
$error = this->getParameter('too_young_error');
return false;
  }
}
 
by excessive demon on 2008-04-16, tagged age  dateofbirth  validation 

sfFileImageValidator

Description:

This images validations is an extention for the sfFileValidator. You can use it for validate uploaded images maximum width and height, minimum width and height and if the images have square dimensions.

<?php
 
/**
 * sfFileImageValidator allows you to apply constraints to image file upload, it extend the sfFileValidator functions.
 *
 * <b>Optional parameters:</b>
 *
 * # <b>max_height</b>         - [none]                                - Maximum images height in pixels.
 * # <b>max_height_error</b>   - [The file height is too large]        - An error message to use when
 *                                                                       images height is too large.
 * # <b>max_width</b>          - [none]                                - Maximum images width in pixels.
 * # <b>max_width_error</b>    - [The file width is too large]         - An error message to use when
 *                                                                       images width is too large.
 * # <b>min_height</b>         - [none]                                - Minimum images height in pixels.
 * # <b>min_height_error</b>   - [The file height is too small]        - An error message to use when
 *                                                                       images height is too small.
 * # <b>min_width</b>          - [none]                                - Minimum images width in pixels.
 * # <b>min_width_error</b>    - [The file width is too small]         - An error message to use when
 *                                                                       images width is too small.
 * # <b>is_square</b>          - [false]                               - The image is a square
 * # <b>is_square_error</b>    - [The file is not a square]            - An error message to use when
 *                                                                       the images is not a square
 *                                                                       (The width size is not equal
 *                                                                       to the height size).
 * @package    symfony
 * @subpackage validator
 * @author     Daniel Santiago 
 */
 
class sfFileImageValidator extends sfFileValidator
{
  /**
   * Executes this validator.
   *
   * @param mixed A file or parameter value/array
   * @param error An error message reference
   *
   * @return bool true, if this validator executes successfully, otherwise false
   */
 
  public function execute(&$value, &$error)
  {
    if (parent::execute($value, $error))
    {
      list($width, $height) = @getimagesize($value['tmp_name']);
 
      // File is not a square
      $is_square = $this->getParameter('is_square');
      if ($is_square && $width != $height)
      {
        $error = $this->getParameter('is_square_error');
 
        return false;
      }
 
      // File height too large
      $max_height = $this->getParameter('max_height');
      if ($max_height !== null && $max_height < $height)
      {
        $error = $this->getParameter('max_height_error');
 
        return false;
      }
 
      // File width too large
      $max_width = $this->getParameter('max_width');
      if ($max_width !== null && $max_width < $width)
      {
        $error = $this->getParameter('max_width_error');
 
        return false;
      }
 
      // File height too small
      $min_height = $this->getParameter('min_height');
      if ($min_height !== null && $min_height > $height)
      {
        $error = $this->getParameter('min_height_error');
 
        return false;
      }
 
      // File width too small
      $min_width = $this->getParameter('min_width');
      if ($min_width !== null && $min_width > $width)
      {
        $error = $this->getParameter('min_width_error');
 
        return false;
      }
 
      return true;
    }
  }
 
  /**
   * Initializes this validator.
   *
   * @param sfContext The current application context
   * @param array   An associative array of initialization parameters
   *
   * @return bool true, if initialization completes successfully, otherwise false
   */
  public function initialize($context, $parameters = null)
  {
    // initialize parent
    parent::initialize($context, $parameters);
 
    // set defaults
    $this->getParameterHolder()->set('max_height',        null);
    $this->getParameterHolder()->set('max_height_error',  'The file height is too large');
    $this->getParameterHolder()->set('max_width',         null);
    $this->getParameterHolder()->set('max_width_error',   'The file width is too large');
    $this->getParameterHolder()->set('min_height',        null);
    $this->getParameterHolder()->set('min_height_error',  'The file height is too small');
    $this->getParameterHolder()->set('min_width',         null);
    $this->getParameterHolder()->set('min_width_error',   'The file width is too small');
    $this->getParameterHolder()->set('is_square',         false);
    $this->getParameterHolder()->set('is_square_error',   'The file is not a square');
 
    $this->getParameterHolder()->add($parameters);
 
    return true;
  }
}
 

How to use it?

In the YAML validation file put this:

  news{photo}:
    file:     yes
    sfFileImageValidator:
      min_height:       100
      min_height_error: 'The image height is too small, it must have minimum 100px'
      min_width:        120
      min_width_error: 'The image width is too small, it must have minimum 120px'
      max_height:       960
      max_height_error: 'The image height is too large, it must have maximum 960px'
      max_width:        450
      max_width_error: 'The image width is too large, it must have maximum 450px'
      is_square:        true
      is_square_error:  'The images must be a square (The height be equal to the width)'
 
      max_size:         256000
      max_size_error:   'The maximum images size is 250Kb'
      mime_types_error: 'We only accept GIF, PNG and JPEG.'
      mime_types:
        - 'image/jpeg'
        - 'image/png'
        - 'image/gif'
 
by Daniel Santiago on 2007-12-26, tagged image  images  validation  validator 

jsValidator integration helper

A real client-side validation with some nice features, without Ajax tricks provided by sfYzAjaxValidationPlugin may be easily done using jsValidator (http://sourceforge.net/projects/jsformutils/) and little helper that follows.

<?php
/**
 * Generates JavaScript code to validate a form using 
 * jsValidator as client-side validation engine (see http://sourceforge.net/projects/jsformutils/)
 * 
 * @param string $targetForm id attribute of the form to be validated
 * @param mixed $options associative array of options
 * @param string $action action to validated, defaults to current
 * 
 * Options are:
 *  stopOnFirstError: boolean, default: false
 *  labelMessageDelimiter: string, default: ' : ',
 *  messageSeparator: string, default: "\n",
 *  messageHeader: string, default: 'These fields are invalid:\n---\n' + 
 *  highlightErrors: boolean, whether to mark erroneous fields 
 *  errorElementClass: string, CSS class name to be applied to wrong fields
 *  highlightLabels: boolean, whether to mark fields or fields' labels
 */
function generate_validator($targetForm, $options, $action)
{
  $NL = "\n";
  $funcPrefix = 'validate_';
  $labelsKey = 'labels';
  $fieldsKey = 'fields';
  $jsCode = '';
 
  $paramHolder = sfContext::getInstance()->getRequest()->getParameterHolder();  
  $rulesFilePath = sfConfig::get('sf_app_module_dir').'/'.$paramHolder->get('module').'/'.
    sfConfig::get('sf_app_module_validate_dir_name').'/';
 
  // Load rules from YAML file.   
  if (file_exists($rulesFilePath.$paramHolder->get('action').'.yml'))
    $rules = sfYaml::load($rulesFilePath.$paramHolder->get('action').'.yml');
  else
    $rules = sfYaml::load($rulesFilePath.$action.'.yml');
 
  // Generate jsValidator compliant rules.  
  $jsRules = array();
  foreach ($rules['fields'] as $fieldId => $validationRule)
  {
    foreach ($validationRule as $validator=>$rule)
    {
      // Remove server-side sfCallbackValidator.
      if ($validator == 'sfCallbackValidator')
      {
        unset($validationRule[$validator]);
        continue;
      }
      // Map Symfony validators to jsValidator.
      $jsvalidator = preg_replace('/^sf(\w+Validator)$/', 'js\\1', $validator);
      if ($jsvalidator != $validator)
      {
        $validationRule[$jsvalidator] = $validationRule[$validator];
        unset($validationRule[$validator]); 
      }
    }
    $jsRules[] = array_merge ( 
      array ('field' => $fieldId, 'label' => $rules['labels'][$fieldId]),
      $validationRule
    );    
  }
 
  // Generate final JavaScript code. 
  $jsCode .= 'function '.$funcPrefix.$targetForm.'()'.$NL;
  $jsCode .= 
  '{'.$NL.
  ' var options = '.json_encode($options).';'.$NL.
  ' var rules = '.json_encode($jsRules).';'.$NL.
  ' var jsv = new jsValidator();'.$NL.
  ' jsv.SetOptions(options);'.$NL.$NL.
  ' if (!jsv.Validate(rules))'.$NL.
  ' {'.$NL.
  '   alert(jsv.GetErrorMessage());'.$NL.
  '   return false;'.$NL.  
  ' }'.$NL.
  ' return true;'.$NL.  
  '}'.$NL;
 
  return javascript_tag($jsCode);
}
?>
 

However, I need to clear this out. Besides placing this code into jsValidatorHelper.php either in modules' lib directory or symfony's one, we need to call it properly in the corresponding view.

First of all, include the helper (use JavaScript as well, jsValidator depends on it)

<?php use_helper('JavaScript', 'jsValidator') ?>
 

Validation is performed based on the same simple rule mechanism that Symfony provides. The only difference is that JavaScript validator needs these rules to be in JSON format and it needs some more options, that configure it's behavior.

<?php
// Here we set validation options.
// For more information please refer to documentation of jsValidator
$options = array(
  'stopOnFirstError' => false,
  'labelMessageDelimiter' => ' : ',
  'messageSeparator' => "\n",
  'messageHeader' => "These fields are invalid:\n---\n",
  'highlightErrors' => true,
  'errorElementClass' => 'errClass',
  'highlightLabels' => true
  ); 
// Output auto-generated JavaScript code.
echo generate_validator('editComment', $options, 'update'); 
?>
 

We also have to set up form to point to our validator before submitting. Note, that callback in onsubmit is named by concatenating "validate_" and form's id attribute.

<?php echo form_tag('comment/update', array('id'=>'editComment','onsubmit' => 'return validate_editComment()')) ?>
 

Each field must be somehow identified in the resulting error message. We achieve this by adding some extra information to <action>.yml configuration file.

# define labels for erroneous fields
labels:
  <field_id>: <field_label_text>
 

There is a limitation to validation *.yml file structure. The syntax should be something like this:

# define labels for erroneous fields
labels:
  author: Author
  email: E-mail
  body: Body
 
fields:
  author:
    required:
      msg: The name field cannot be left blank
  email:
    sfEmailValidator:
      email_error: The email address is not valid.
  body:
    required:
      msg: The text field cannot be left blank
 
fillin:
  enabled:       on
 

And finally, don't forget to place jsValidator into /web/js folder, and include it in view.yml

<actionTemplate>:
  javascripts: [jsvalidator]
 

Feel free to modify the snippet code and validator to achieve best results!

by Alex Oroshchuk on 2007-12-04, tagged helper  javascript  validation 
(1 comment)

Disable fillin filter after validation success

When using fillin filter, after validation success (all form fields are ok) if you want display page without form, fillin filter is still enabled and throws exception "Exception - No form found in this page". Use this snippet to prevent it.

in sfFillInFormFilter.class.php file

  public function execute($filterChain)
  {
    // execute next filter
    $filterChain->execute();
 
    $context  = $this->getContext();
    $response = $context->getResponse();
    $request  = $context->getRequest();
 
    // add these two lines
    if(! $request->hasErrors())
        return;
 
by jarecky on 2007-11-20, tagged fillin  filter  form  validation 

Password Strength validator

This its a password strength validator, with ajax request for checking the password field.

First create a validator in lib/validators/sfPasswordStrengthValidator.class.php

<?php
class sfPasswordStrengthValidator extends sfValidator
{
    public function execute (&$value, &$error)
    {
        $weakness = $this->Password_Strength($value);
 
        if($weakness==1) {
            $error = $this->getParameter('strength_error');
            return false;
        }
 
        return $weakness;
    }
 
    public function initialize ($context, $parameters = null)
    {
        // Initialize parent
        parent::initialize($context);
 
        // Set default parameters value
        $this->setParameter('strength_error', 'Weak password');
 
        // Set parameters
        $this->getParameterHolder()->add($parameters);
 
        return true;
    }
        // Thanks for: Alix Axel Weblog
    // URL: http://www.alixaxel.com/wordpress/wp-content/2007/06/Password_Strength.phps
    function Password_Strength($password, $username = null)
    {
        if (!empty($username))
        {
            $password = str_replace($username, '', $password);
        }
 
        $strength = 0;
        $password_length = strlen($password);
 
        if ($password_length < 5)
        {
            return $strength;
        }
 
        else
        {
            $strength = $password_length * 4;
        }
 
        for ($i = 2; $i <= 4; $i++)
        {
            $temp = str_split($password, $i);
 
            $strength -= (ceil($password_length / $i) - count(array_unique($temp)));
        }
 
        preg_match_all('/[0-9]/', $password, $numbers);
 
        if (!empty($numbers))
        {
            $numbers = count($numbers[0]);
 
            if ($numbers >= 3)
            {
                $strength += 5;
            }
        }
 
        else
        {
            $numbers = 0;
        }
 
        preg_match_all('/[|!@#$%&*\/=?,;.:\-_+~^ยจ\\\]/', $password, $symbols);
 
        if (!empty($symbols))
        {
            $symbols = count($symbols[0]);
 
            if ($symbols >= 2)
            {
                $strength += 5;
            }
        }
 
        else
        {
            $symbols = 0;
        }
 
        preg_match_all('/[a-z]/', $password, $lowercase_characters);
        preg_match_all('/[A-Z]/', $password, $uppercase_characters);
 
        if (!empty($lowercase_characters))
        {
            $lowercase_characters = count($lowercase_characters[0]);
        }
 
        else
        {
            $lowercase_characters = 0;
        }
 
        if (!empty($uppercase_characters))
        {
            $uppercase_characters = count($uppercase_characters[0]);
        }
 
        else
        {
            $uppercase_characters = 0;
        }
 
        if (($lowercase_characters > 0) && ($uppercase_characters > 0))
        {
            $strength += 10;
        }
 
        $characters = $lowercase_characters + $uppercase_characters;
 
        if (($numbers > 0) && ($symbols > 0))
        {
            $strength += 15;
        }
 
        if (($numbers > 0) && ($characters > 0))
        {
            $strength += 15;
        }
 
        if (($symbols > 0) && ($characters > 0))
        {
            $strength += 15;
        }
 
        if (($numbers == 0) && ($symbols == 0))
        {
            $strength -= 10;
        }
 
        if (($symbols == 0) && ($characters == 0))
        {
            $strength -= 10;
        }
 
        if ($strength < 0)
        {
            $strength = 0;
        }
 
        if ($strength > 100)
        {
            $strength = 100;
        }
 
        return $strength;
    }
}
 

Then in your view template use:

<?php echo observe_field('password', array(
                          'update'   => 'passwordStatus',
                          'url'      => 'sfGuardAuth/checkPasswordStrength',
                          'with' => "'id='+encodeURIComponent($('password').value)",
                          'loading'=>"Element.show('indicator_passwordstatus')",
                          'complete'=>"Element.hide('indicator_passwordstatus');Element.show('passwordStatus');",
                      )) ?>
 

That code will monitor changes at password input field, and submit the updated value to the defined route.

Then add the following to your actions file:

public function executeCheckPasswordStrength() {
        $password = $this->getRequestParameter('id');
        $strengthValidator = new sfPasswordStrengthValidator();
        $strengthValidator->initialize($this->getContext());
        $error='none';
        $score = $strengthValidator->execute($password,$error);
        if(!$score)
        return $this->renderText('too short');
        if($score < 20) {
            return $this->renderText('not weak');
        } else if($score < 50) {
            return $this->renderText('relevant');
        } else {
            return $this->renderText('strong');
        }
 
        return true;
    }
 

Cheers

by Lucas Peres on 2007-10-05, tagged validation  validator 

YML validation in actions

Currently i had to do different validations of data depending the data entered by the user.

The solution i found, using the symfony built-in yml validation was:

action:

$validated = true;
$validationConfig = $this->getModuleName().'/'.sfConfig::get('sf_app_module_validate_dir_name').'/'.$validationFile.'.yml';
 
if (null !== $validateFile = sfConfigCache::getInstance()->checkConfig(sfConfig::get('sf_app_module_dir_name').'/'.$validationConfig, true))
{
    $context = $this->getContext();
    $validatorManager = new  sfValidatorManager();
        $validatorManager->initialize($context);
    require($validateFile);
    $validated = $validatorManager->execute();
}
 

refactoring:

public function validateParamsYml($validationFile) {
        $validated = true;
        $validationConfig = $this->getModuleName().'/'.sfConfig::get('sf_app_module_validate_dir_name').'/'.$validationFile.'.yml';
 
        if (null !== $validateFile = sfConfigCache::getInstance()->checkConfig(sfConfig::get('sf_app_module_dir_name').'/'.$validationConfig, true))
        {
            $context = $this->getContext();
            $validatorManager = new sfValidatorManager();
            $validatorManager->initialize($context);
            require($validateFile);
            $validated = $validatorManager->execute();
        }
        return $validated;
    }
 

Then you can call any validations rules at your action:

public function executeXXX() {
  [...]
  [...]
  $this->validateParamsYml('yml_1');
  $this->validateParamsYml('yml_2');
  [...]
}
 

and finally check if everything was validated then forward/save the data:

if($this->getRequest()->hasErrors()) {
            return $this->handleErrorXXXX();
        }
 

Cheers

by Lucas Peres on 2007-10-02, tagged validation 

Simple CAPTCHA

Hi!

This code is about a very simple CAPTCHA validation. I am pretty sure that you all know what CAPTCHA is, so I won't explain it. This CAPTCHA is based on the color-blindness test that operates with various colored balls.

The class:

class Captcha
{
    protected $value;
    protected $image_name;
 
    public function __construct( $image_name )
    {
        $this->image_name = $image_name;
    }
 
    public static function createNumber( $length = 4 )
    {
        $num = "";
        for ( $i = 0; $i < $length; $i++ )
        {
            $r = mt_rand( 0, 9 );
            $num = $num.$r;
        }
        return $num;
    }
 
    public static function createString( $length = 4 )
    {
        $val = "";
        $values = "abcdefghijklmnopqrstuvwyz";
        for ( $i = 0; $i < $length; $i++ )
        {
            $r = mt_rand( 0, 24 );
            $val = $val.$values[$r];
        }
        return $val;        
    }
 
    public function setValue( $value )
    {
        $this->value = $value;
    }
 
    public function getValue( )
    {
        return $this->value;
    }
 
    public function showImage( $channel = null )
    {
        $im = imageCreate( 160, 60 );
        $white = imagecolorallocate( $im, 255, 255, 255 );
        $black = imagecolorallocate( $im, 0, 0, 0 );
 
        $intensity = rand ( 200, 230 );
 
        if ( !$channel )
        {
            $channel = rand ( 1, 3 );
        }
        $ratio = rand ( 80, 100 );
 
        switch ( $channel )
        {
                case 1:
                        $colorBgR = $intensity * $ratio / 100;
                        $colorBgG = $intensity * ( 100 - $ratio ) / 100;
                        $colorBgB = 0;
 
                        for ( $i = 0; $i < 5; $i++ )
                        {
                                $shift = rand( 100 - ( $i+1)*3, 100 + ( $i+1)*3 );
                                $new = $shift * $colorBgR / 100;    
 
                                $bgColors[] = iMagecolorallocate( $im, $new, $intensity-$new, 0 );
                        }
 
                        for ( $i = 0; $i < 3; $i++ )
                        {
                                $shift = rand( 100 - ( $i+1)*7, 100 + ( $i+1)*7 );
                                $new = $shift * $colorBgR / 100;    
 
                                $bgColors[] = iMagecolorallocate( $im, $intensity-$new, $new, 0 );
                        }
                break;
 
                case 2:
                        $colorBgG = $intensity * $ratio / 100;
                        $colorBgB = $intensity * ( 100 - $ratio ) / 100;
                        $colorBgR = 0;
 
                        for ( $i = 0; $i < 5; $i++ )
                        {
                                $shift = rand( 100 - ( $i+1)*3, 100 + ($i+1)*3 );
                                $new = $shift * $colorBgR / 100;    
 
                                $bgColors[] = iMagecolorallocate( $im, 0, $new, $intensity-$new );
                        }
 
                        for ( $i = 0; $i < 3; $i++ )
                        {
                                $shift = rand( 100 - ( $i+1)*7, 100 + ( $i+1)*7 );
                                $new = $shift * $colorBgR / 100;    
 
                                $bgColors[] = iMagecolorallocate( $im, 0 , $intensity-$new, $new );
                        }
 
                case 3:
                        $colorBgB = $intensity * $ratio / 100;
                        $colorBgR = $intensity * ( 100 - $ratio ) / 100;
                        $colorBgG = 0;
 
                        for ( $i = 0; $i < 5; $i++ )
                        {
                                $shift = rand( 100 - ( $i+1)*3, 100 + ( $i+1)*3 );
                                $new = $shift * $colorBgR / 100;    
 
                                $bgColors[] = iMagecolorallocate( $im, $intensity-$new, 0, $new );
                        }
 
                        for ( $i = 0; $i < 3; $i++ )
                        {
                            // to make it harder do symmetric range ^^ 
                                $shift = rand( 100 , 100 + ( $i+1)*20 );
                                $new = $shift * $colorBgR / 100;    
 
                                $bgColors[] = iMagecolorallocate( $im, $new , 0, $intensity-$new );
                        }
                break;
        }
 
 
 
 
        $bgColor = imagecolorallocate( $im, $colorBgR, $colorBgG, $colorBgB );
 
 
        for ( $i = 0; $i < strlen( $this->value ); $i++ )
        {
            $rotate = rand(-15, 15);
 
            $fontSize = rand(28, 36);
            imagettftext($im, $fontSize ,$rotate , 10+$i*31, 50, $black, "impact.ttf",$this->value[$i] ); 
        }
 
 
 
 
        for ( $i = 0; $i < 160; $i++ )
        {
                for ( $j = 0; $j  < 60; $j++ )
                {
                        $pos = rand( 0, 7 );
 
                        if ( imagecolorat( $im, $i, $j ) == $black )
                        {
                                $pos = rand( 6, 7 );
 
 
 
                                imagesetpixel( $im, $i, $j, $bgColors[$pos] );
                        }
                        else
                        {
                                imagesetpixel( $im, $i, $j, $bgColors[$pos] );
                        }
                }
        }
 
        imagejpeg( $im, "images/".$this->image_name.".jpg" );
        imagedestroy( $im );
        echo" <img src='/images/".$this->image_name.".jpg' style='border: 1px solid #000;' width='160' >";
    }
}

The random string/number generator functions are static, so that they can be used anytime.

The string to be shown is stored in the class member value, this is to be set manually using setValue( $value ).

Function showImage renders and shows the image. It uses the 3 base colors Red Green and Blue ( $channel ) to generate the background color. This can be set as a parameter so images get only green ( $channel = 2 ) background, this my be important to exclude variations that are hard to read.

The logic is the following:

In the action you use to render the template where the CAPCTHA is to be shown, generate a value for it ( you can use the createString or createNumber functions ). Flash this value in the action ( $this->setFlash( 'captcha', $this->value ), and "pass" it to the template ( set the value as a member of the action $this->value = $value ).

In the template create an instance of class Captcha. Set the value by using the setValue function. And call showImage().

Create an input field on the template, and override the validate() function in your action.class.php. The logic is simple we have two strings and we want to know if they match or not. So we make an insatnce of confirm validaor, set the check parameter to the original captcha value ( $this->getFlash( 'captcha' ) ) and execute the validator on the input field's post value ( $this->getRequestParameter('name_of_input_field' ) ).

Many of the parameters that are actually fixed in the code should be members of the class and should have proper set/get functions. I've played with this for a short period of time, so feel free to complement the code.

by Kormany Gabor on 2007-06-30, tagged captcha  validation 

Pass multiple parameters to sfCallbackValidator

I love sfCallbackValidator and use it all the time, but found it was somewhat limiting in that only the value being validated could be passed to the function or method that is doing the validating. So, I've extended it, overriding the execute method:

myCallbackValidator.class.php:

<?php
 
class myCallbackValidator extends sfCallbackValidator {
 
  public function execute(&$value, &$error) {
 
    $callback = $this->getParameterHolder()->get('callback');
    if (!call_user_func($callback, $value, $this->getParameterHolder()->get('parameters'))) {
      $error = $this->getParameterHolder()->get('invalid_error');
      return false;
    }
    return true;
 
  }
 
}

You can specify the parameters in your validation yaml file like so...

  birthdate:
    myCallbackValidator:
      callback:       [myValidationTools, birthDate]
      invalid_error:  Birthdate is invalid. You must be at least 18 years old to apply.
      parameters:
        min_age:      18

And then the parameters can be accessed in the callback function like so:

  public static function birthDate($string, $params = null) {
    if (isset($params['min_age'])) {
      ... etc...
by east3rd on 2007-06-27, tagged callback  validation  validator 
(2 comments)

sfCustomUniqueValidator:

This snippet allow to check if an entry allready exists in a database but, at the difference of the sfUniqueValidator, you can provide as many fields as desired to perform the verification.

Installation:

The first thing you need to do is to create the file sfCustomUniqueValidator.php in your project lib directory:

<?php
  /**
 * sfCustomUniqueValidator checks if a record exist in the database with all the mentionned fields.
 *
 * ex: Check if a companie with company_name exist in country_id 
 *   class:            sfCustomUniqueValidator
 *   param:
 *     class:          Companies    //the class on which the search is performed
 *     nb_fields:      2            //the number of fields on which the comparison is done
 *     field_1:        company_name //First field of the comparison
 *     field_2:        country_id   //Other country for the comparison
 *
 * @package    lib
 * @author     Joachim Martin
 * @date       15/06/2007
 */
 
class sfCustomUniqueValidator extends sfValidator {
 
   /**
   * Executes this validator.
   *
   * @param mixed A file or parameter value/array
   * @param error An error message reference
   *
   * @return bool true, if this validator executes successfully, otherwise false
   */
 
    public function execute(&$value, &$error) {
 
        $className  = $this->getParameter('class').'Peer';
 
        //Get fields number
        $nb_fields = $this->getParameter('nb_fields');
 
        //Define new criteria       
        $c = new Criteria();
 
        //Loop on the fields
        for($i = 1; $i <= $nb_fields ; $i++) {
            //Retrieve field_$i 
            $check_param = $this->getParameterHolder()->get("field_$i");
            $check_value = $this->getContext()->getRequest()->getParameter($check_param);
 
            //If check value defined        
            if ($check_value != '') {   
                //Adding field to the criteria
                $columnName = call_user_func(array($className, 'translateFieldName'), $check_param, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_COLNAME);
                $c->add($columnName, $check_value);
            }
        }
 
        $object = call_user_func(array($className, 'doSelectOne'), $c);
 
        if ($object)
        {
          $tableMap = call_user_func(array($className, 'getTableMap'));
          foreach ($tableMap->getColumns() as $column)
          {
            if (!$column->isPrimaryKey())
            {
              continue;
            }
 
            $method = 'get'.$column->getPhpName();
            $primaryKey = call_user_func(array($className, 'translateFieldName'), $column->getPhpName(), BasePeer::TYPE_PHPNAME, BasePeer::TYPE_FIELDNAME);
            if ($object->$method() != $this->getContext()->getRequest()->getParameter($primaryKey))
            {
              $error = $this->getParameter('custom_unique_error');
 
              return false;
            }
          }
        }
 
        return true;
    } 
 
    public function initialize ($context, $parameters = null) {
        // initialize parent
        parent::initialize($context);
 
        //Set default parameters value
        $this->setParameter('custom_unique_error','The value is not unique');
 
        $this->getParameterHolder()->add($parameters);
 
        // check parameters
        if (!$this->getParameter('class'))
        {
          throw new sfValidatorException('The "class" parameter is mandatory for the sfCustomUniqueValidator validator.');
        }
 
        if (!$this->getParameter('nb_fields'))
        {
          throw new sfValidatorException('The "nb_fields" parameter is mandatory for the sfCustomUniqueValidator validator.');
        }
 
        return true;
    }
}

Usage:

The following code check if a companie with the same name and same activity exists in the same country

sfCustomUniqueValidator:
      class:                   Companies
      nb_fields:               3
      field_1:                 company_name
      field_2:                 activity_id
      field_3:                 country_id
      custom_unique_error:     This company already exist for this country

class: the model to test

nb_fields: how many fields will be checked

field_x: a field to test, obviously you need to have as many field_x as the nb_fields value

custom_unique_error: your error message

This is my very first contribution to symfony so feel free to comment/optimize.

by joachim martin on 2007-06-21, tagged unique  validation  validator 
(5 comments)

easy cookie validation for user login systems

If you're building a site with a user login system (like Askeet) and your PHP is configured to store session variables in a client-side cookie, the following snippet will improve the usability for users who have disabled cookies.

The following example assumes you already have at least a simple user login system on your site. If not, check out the Askeet tutorial for a great example to get you started.

All users who have disabled cookies will be unable to log into any site that relies on client-side cookies to store session variables. If you don't validate cookies and provide notification, these users will never know why they couldn't log in to your site.

Try the following if you'd like to see this firsthand.

Unfortunately, Askeet also provides the perfect example here as well. (Sorry guys!)

Not much happened, right? You're not logged in and you don't know why. (Well, you do now.)

What we're going to do is augment an existing login system to provide users with notification that your site requires cookies.

To do this, we're going to attempt to set a cookie on the login page and verify that it was created once the login form is submitted. Since a cookie is stored only after the page is loaded, it takes two pages to validate the user's setting. Fortunately, the login process takes two pages!

Edit your existing login action code by adding the following two setCookie() functions.

The first setCookie() sets the validation cookie when the login action is first loaded. (If you use your login on several pages as a component/partial, move this first setCookie() to the login form component.)

The second setCookie() deletes the validation cookie upon successful login. If this cookie is not deleted, the validation could return a false positive if the user disables cookies at a later date. (When cookies are disabled, new cookies can't be written, but old cookies can be read.)

module_dir/actions/actions.class.php:

public function executeLogin()
{
  if ($this->getRequest()->getMethod() != sfRequest::POST)
  {      
    sfContext::getInstance()->getResponse()->setCookie('cookies_enabled', 'true', time()+60*60*24, '/');    
  }
  else
  {
    sfContext::getInstance()->getResponse()->setCookie('cookies_enabled', '', 0, '/');    
    $this->redirect('@homepage');
  }
}

Enable cookie validation by adding the following code to your existing login validation. The validator takes two parameters, cookie_name and cookie_error. The cookie_name parameter is, oddly enough, the name of the cookie we'll use to validate and it should match the cookie set in the above code.

The other parameter, cookie_error, is the error message that will be returned if the user has cookies disabled. Typically, validation errors are phrases like "Invalid username" or "Password must contain 6-8 characters". But we're going to use "cookies_disabled" and I'll show you why in a few minutes.

IMPORTANT: The cookie validation should occur first.

module_dir/validate/login.yml:

methods:
  post: [username]
 
names:
  username:
    required:     true
    required_msg: 
    validators:   [cookiesEnabledValidator, userValidator]
 
cookiesEnabledValidator:
  class:          myCookiesEnabledValidator
  param:
    cookie_name:  cookies_enabled
    cookie_error: cookies_disabled
 
userValidator:
  class:          myLoginValidator
  param:
    password:     password
    username_error: Invalid username.
    password_error: Invalid password.

Copy the following code to one of your lib directories. Since it only deals with the login action, I choose to keep it in my user module's lib directory.

module_dir/lib/myCookiesEnabledValidator.class.php:

class myCookiesEnabledValidator extends sfValidator
{    
  public function initialize($context, $parameters = null)
  {
    // initialize parent
    parent::initialize($context);
 
    // set defaults
    $this->setParameter('cookie_name', sfContext::getInstance()->getStorage()->getParameter('session_name'));
    $this->setParameter('cookie_error', 'This site requires cookies.');
    $this->getParameterHolder()->add($parameters);
 
    return true;
  }
 
  public function execute(&$value, &$error)
  {    
    if (sfContext::getInstance()->getRequest()->getCookie($this->getParameter('cookie_name')) === null)
    {
      $error = $this->getParameter('cookie_error');
      return false;
    }
    return true;
  }
}

Now, since it takes two pages to set and read a cookie it wouldn't make sense to return the user to the form right away. If they enable their cookies they'll still have to submit the form twice before they'll login successfully. I prefer to send them to a page which notifies them that the site requires cookies and explains how they can enable them. (Plus, this gives us the extra click we need to set that validation cookie!)

So we'll check for the cookie validation error before returning to the form so we can redirect the user to our help page if necessary. The error we check for in the username parameter must match the cookie_error we defined in the login.yml.

module_dir/actions/actions.class.php:

public function handleErrorLogin()
{
  if (sfContext::getInstance()->getRequest()->getError('username') == 'cookies_disabled')
  {
    $this->redirect('@about_cookies');
  }
  return sfView::SUCCESS;
}

So that's it. Throw in an about cookies page and you're all set.

If I made any mistakes, I apologize. It's 5am on a school night.

by dave furf on 2007-05-11, tagged cookie  login  session  storage  usability