Snippets

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

Navigation

Snippets tagged "validator latitude longitude" Snippets tagged "validator latitude longitude"

Latitude Longitude Validator

A Latitude Longitude validator that allows you to specify very exact constraints for the co-ordinates that are submitted.

<?php
 
/*
 * This file is part of the symfony package.
 * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
 
/**
 * myLatLngValidator 
 * 
 * @description This class validator will validate the entered latitude and
 * longitudes submitted using a form or other method.
 * @subpackage validator
 * @author Nathan Rzepecki
 * @created 21/4/2008
 */
class myLatLngValidator 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)
  {
//
//      echo '<pre>value is: '.$value.'</pre>';
//      echo '<pre>min_type: '.$this->getParameterHolder()->get('min_type').'</pre>';
//      echo '<pre>max type: '.$this->getParameterHolder()->get('max_type').'</pre>';
//      echo '<pre>min: '.$this->getParameterHolder()->get('min').'</pre>';
//      echo '<pre>max: '.$this->getParameterHolder()->get('max').'</pre>';
//      exit;
 
//      echo '<pre>value: '.$value.'</pre>';
//      echo '<pre>max type: '.$this->getParameterHolder()->get('max_type').'</pre>';
//      echo '<pre>min type: '.$this->getParameterHolder()->get('min_type').'</pre>';
//      echo '<pre>min: '.$this->getParameterHolder()->get('min').'</pre>';
//      echo '<pre>max: '.$this->getParameterHolder()->get('max').'</pre>';
//      echo '<pre>'..'</pre>';
 
    /*
     * If the min_type was set
     */
      if ($this->getParameterHolder()->get('min_type') == '+')
      {
         // type was positive
         $re = '/^\d{1,3}(\.(\d*)){0,1}$/';
         if (!preg_match($re, $value))
         {
           $error = $this->getParameterHolder()->get('min_type_error');
           return false;
         } // end if
      }
      elseif ($this->getParameterHolder()->get('min_type') == '-')
      {
        // type was negative
        $re = '/^-\d{1,3}(\.(\d*)){0,1}$/';
         if (!preg_match($re, $value))
         {
           $error = $this->getParameterHolder()->get('min_type_error');
           return false;
         } // end if
      }
      else
      {
        // don't care if it is plus or minus
         $re = '/^-{0,1}\d{1,3}(\.(\d*)){0,1}$/';
         if (!preg_match($re, $value))
         {
           $error = $this->getParameterHolder()->get('min_type_error');
           return false;
         } // end if
      } // end if elseif else
 
 
    /*
     * if max_type was set
     */            
      if ($this->getParameterHolder()->get('max_type') == '+')
      {
         // type was positive
         $re = '/^\d{1,3}(\.(\d*)){0,1}$/';
         if (!preg_match($re, $value))
         {
           $error = $this->getParameterHolder()->get('max_type_error');
           return false;
         } // end if
      }
      elseif ($this->getParameterHolder()->get('max_type') == '-')
      {
        // type was negative
        $re = '/^-\d{1,3}\.{0,1}\d+$/';  // negative digit
         if (!preg_match($re, $value))
         {
           $error = $this->getParameterHolder()->get('max_type_error');
           return false;
         } // end if
      }
      else
      {
        // don't care if it is plus or minus
         $re = '/^-{0,1}\d{1,3}(\.(\d*)){0,1}$/';
         if (!preg_match($re, $value))
         {
           $error = $this->getParameterHolder()->get('max_type_error');
           return false;
         } // end if
      } // end if elseif else
 
 
    /*
     * if max value was set
     */
     if ($this->getParameterHolder()->get('max'))
     {
       $re = '/^-\d{1,3}(\.(\d*)){0,1}$/';  // negative digit
       if (preg_match($re, $this->getParameterHolder()->get('max')))
       {
          // yes its a negative so remove the prefix -
          $maxValue = substr($this->getParameterHolder()->get('max'), 1);
 
          if (!preg_match($re, $value))
          {
            $error = $this->getParameterHolder()->get('max_type_error');
            return false;
          }
          else
          {
            // both the max value and the value are negative so test that the value is less than the max value
            if (!($maxValue > substr($value, 1)))
            {
              $error = $this->getParameterHolder()->get('max_error');
              return false;
            } // end if 
          } // end if else          
       } 
       else
       {
         $maxValue = $this->getParameterHolder()->get('max');
 
         $re = '/^\d{1,3}(\.(\d*)){0,1}$/';  // positive digit
         if (preg_match($re, $value))
         {
           // Yeah the value is a positive so make sure its within range
           if (!($maxValue > $value))
           {
              $error = $this->getParameterHolder()->get('max_error');
              return false;
           } // end uf
         }
         else
         {
           $error = $this->getParameterHolder()->get('max_type_error');
           return false;
         } // end if else positive
 
       }// end if
 
     } // end if
 
 
    /*
     * if min value was set
     */
     if ($this->getParameterHolder()->get('min'))
     {
       $re = '/^-\d{1,3}(\.(\d*)){0,1}$/';  // negative digit
       if (preg_match($re, $this->getParameterHolder()->get('min')))
       {
          // yes its a negative so remove the prefix -
          $minValue = substr($this->getParameterHolder()->get('min'), 1);
 
          if (!preg_match($re, $value))
          {
            // the min value was negative so the value should be nexative. This is not so error it
            $error = $this->getParameterHolder()->get('min_type_error');
            return false;
          }
          else
          {
            // both the max value and the value are negative so test that the value is less than the max value
            if (!(substr($value, 1) > $minValue))
            {
              $error = $this->getParameterHolder()->get('min_error');
              return false;
            } // end if 
          } // end if else          
       } 
       else
       {
         $minValue = $this->getParameterHolder()->get('min');
 
         $re = '/^\d{1,3}(\.(\d*)){0,1}$/';  // positive digit
         if (preg_match($re, $value))
         {
           // Yeah the value is a positive so make sure its within range
           if (!($value > $minValue))
           {
              $error = $this->getParameterHolder()->get('min_error');
              return false;
           } // end if
         }
         else
         {
           $error = $this->getParameterHolder()->get('min_type_error');
           return false;
         } // end if else positive
 
       }// end if
 
     } // end if
 
    // was not returned out of any of the above so must be good
    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',            null);
    $this->getParameterHolder()->set('max_error',      'The lat / lng is not within range');
    $this->getParameterHolder()->set('max_type',       null);
    $this->getParameterHolder()->set('max_type_error', 'Your lat / lng did not match the specified type');
    $this->getParameterHolder()->set('min',            null);
    $this->getParameterHolder()->set('min_error',     'The lat / lng is not within range');
    $this->getParameterHolder()->set('min_type',       null);
    $this->getParameterHolder()->set('min_type_error', 'Your lat / lng did not match the specified type');
    //$this->getParameterHolder()->set('', null);
 
    $this->getParameterHolder()->add($parameters);
 
    return true;
  }// end function
} // end class myLatLngValidator
 

You use it like so.

  latitude:
    required: 
      msg:  Please enter the latitude or position the marker where your farm is
    myLatLngValidator:
      min: -9
      min_error: Your entered latitude is not within min range
      min_type: '-'
      min_type_error: Your latitude should be in the negative range
      max: -45
      max_error: Your entered latitude is not within max range
      max_type: '-'
      max_type_error: Your latitude should be in the negative range  
 
  longitude:
    required:
      msg: Please enter the longitude or position the marker where your farm is
    myLatLngValidator:
      min: 111
      min_error: Your longitude is not within min range
      min_type: '+'
      min_type_error: Your longitude should be in the positive range
      max: 154
      max_error: Your longitude is not within max range
      max_type: '+'
      max_type_error: Your longitude should be in the positive range
 

I may clean it up in the future. This was so I could constrain the points to be within Australia.

by lionslair on 2008-04-22, tagged latitude  longitude  validator