Zikula_Core
[ class tree: Zikula_Core ] [ index: Zikula_Core ] [ all elements ]

Source for file ValidationUtil.class.php

Documentation is available at ValidationUtil.class.php

  1. <?php
  2. /**
  3.  * Zikula Application Framework
  4.  *
  5.  * @copyright Robert Gasch
  6.  * @link http://www.zikula.org
  7.  * @version $Id: ValidationUtil.class.php 24342 2008-06-06 12:03:14Z markwest $
  8.  * @license GNU/GPL - http://www.gnu.org/copyleft/gpl.html
  9.  * @author Robert Gasch rgasch@gmail.com
  10.  * @package Zikula_Core
  11.  */
  12.  
  13. /**
  14.  * ValidationUtil
  15.  *
  16.  * @package Zikula_Core
  17.  * @subpackage ValidationUtil
  18.  */
  19. {
  20.     /**
  21.      * Validate a specific field using the supplied control parameters
  22.      *
  23.      * @param objectType    The string object type
  24.      * @param object        The object to validate
  25.      * @param field         The field to validate
  26.      * @param required      whether or not the field is required
  27.      * @param cmp_op        The compare operation to perform
  28.      * @param cmp_value     The value to compare the supplied field value to. If the value starts with a ':', the argument is used as an object access key.
  29.      * @param err_msg       The error message to use if the validation fails
  30.      *
  31.      * @return true/false value indicating whether the field validation passed or failed
  32.      */
  33.     function validateField ($objectType$object$field$required$cmp_op$cmp_value$err_msg)
  34.     {
  35.         if (!is_array($object)) {
  36.             return pn_exit ('ValidationUtil::validateField: object is not an array ... ');
  37.         }
  38.  
  39.         if (!$field{
  40.             return pn_exit ('ValidationUtil::validateField: empty field supplied ... ');
  41.         }
  42.  
  43.         if (!$err_msg{
  44.             return pn_exit ('ValidationUtil::validateField: empty error message supplied ... ');
  45.         }
  46.  
  47.         $rc true;
  48.  
  49.         // if this field already has an error, don't perform further checks
  50.         if (isset($_SESSION['validationErrors'][$objectType][$field])) {
  51.             return $rc;
  52.         }
  53.  
  54.         if ($required{
  55.             if (!isset($object[$field]|| $object[$field=== '' || $object[$field=== '0'{
  56.                 $rc false;
  57.             }
  58.         }
  59.  
  60.         if ($rc && $object[$field])
  61.         {
  62.             $postval $object[$field];
  63.             $testval $cmp_value;
  64.             if (substr($testval01== ':'// denotes an object access key
  65.             {
  66.                 $v2   substr($testval1);
  67.                 $testval $object[$v2];
  68.             }
  69.  
  70.             //print "$postval $cmp_op $testval";
  71.  
  72.             if ($cmp_op == 'eq'{
  73.                 $rc ($postval === $testval);
  74.             }
  75.             else
  76.             if ($cmp_op == 'neq'{
  77.                 $rc ($postval != $testval);
  78.             }
  79.             else
  80.             if ($cmp_op == 'gt'{
  81.                 $rc ($postval !== '' && is_numeric($postval&& $postval $testval);
  82.             }
  83.             else
  84.             if ($cmp_op == 'gte'{
  85.                 $rc ($postval !== '' && is_numeric($postval&& $postval >= $testval);
  86.             }
  87.             else
  88.             if ($cmp_op == 'lt'{
  89.                 $rc ($postval !== '' && is_numeric($postval&& $postval $testval);
  90.             }
  91.             else
  92.             if ($cmp_op == 'lte'{
  93.                 $rc ($postval !== '' && is_numeric($postval&& $postval <= $testval);
  94.             }
  95.             else
  96.             if ($cmp_op == 'url'{
  97.                 $rc pnVarValidate($postval'url');
  98.             
  99.             else
  100.             if ($cmp_op == 'email'{
  101.                 $rc pnVarValidate($postval'email');
  102.             
  103.             else
  104.             if ($cmp_op == 'valuearray'{
  105.                 if (!is_array($testval)) {
  106.                     return pn_exit ('ValidationUtil::validateField: Testval for test valuearray is not an array ... ');
  107.                 }
  108.                 $rc in_array($postval$testval);
  109.             
  110.             else 
  111.             if ($cmp_op == 'noop' || !$cmp_op{
  112.                 if (!$required{
  113.                     return pn_exit ("ValidationUtil::validateField: invalid cmp_op [$cmp_op] supplied for non-required field [$field] ... ");
  114.                 }
  115.                 $rc true;
  116.             else {
  117.                 return pn_exit ("ValidationUtil::validateField: invalid cmp_op [$cmp_op] supplied for field [$field] ... ");
  118.             }
  119.         }
  120.  
  121.         if ($rc === false)
  122.         {
  123.             if (!isset($_SESSION['validationErrors'][$objectType][$field])) {
  124.                 $_SESSION['validationErrors'][$objectType][$field$err_msg;
  125.             }
  126.         }
  127.  
  128.         return $rc;
  129.     }
  130.  
  131.  
  132.     /**
  133.      * Validate a specific field using the supplied control parameters
  134.      *
  135.      * @param object            The object to validate
  136.      * @param validationControl The structured validation control array
  137.      *
  138.      *  The expected structure for the validation array is as follows:
  139.      *  $validationControl[] = array ('field'         =>  $fieldname,
  140.      *                                'required'      =>  true/false,
  141.      *                                'cmp_op'        =>  eq/neq/lt/lte/gt/gte/url/email/valuearray/noop,
  142.      *                                'cmp_value'     =>  $value
  143.      *                                'err_msg'       =>  $errorMessage);
  144.      *
  145.      *  The noop value for the cmp_op field is only valid if the field is not required
  146.      *
  147.      * @return true/false value indicating whether the field validation passed or failed
  148.      */
  149.     function validateFieldByArray ($object$validationControl)
  150.     {
  151.         $objType $validationControl['objectType'];
  152.         $field   $validationControl['field'];
  153.         $req     $validationControl['required'];
  154.         $cmp_op  $validationControl['cmp_op'];
  155.         $cmp_val $validationControl['cmp_value'];
  156.         $err_msg $validationControl['err_msg'];
  157.  
  158.         return ValidationUtil::validateField ($objType$object$field$req$cmp_op$cmp_val$err_msg);
  159.     }
  160.  
  161.  
  162.     /**
  163.      * Validate a specific field using the supplied control parameters
  164.      *
  165.      * @param objectType         The string object type
  166.      * @param object             The object to validate
  167.      * @param validationControls The array of structured validation control arrays
  168.      *
  169.      *  The expected structure for the validation array is as follows:
  170.      *  $validationControls[] = array ('field'         =>  $fieldname,
  171.      *                                 'required'      =>  true/false,
  172.      *                                 'cmp_op'        =>  eq/neq/lt/lte/gt/gte/noop,
  173.      *                                 'cmp_value'     =>  $value
  174.      *                                 'err_msg'       =>  $errorMessage), ...);
  175.      *
  176.      *  The noop value for the cmp_op field is only valid if the field is not required
  177.      *
  178.      * @return true/false value indicating whether the object validation passed or resulted in errors.
  179.      */
  180.     function validateObject ($objectType$object$validationControls)
  181.     {
  182.         $rc true;
  183.  
  184.         foreach ($validationControls as $vc)
  185.         {
  186.             $t ValidationUtil::validateFieldByArray ($object$vc);
  187.             if ($t === false{
  188.                 $rc false;
  189.             }
  190.         }
  191.  
  192.         if (!$rc{
  193.             $_SESSION['validationFailedObjects'][$objectType$object;
  194.         }
  195.  
  196.         return $rc;
  197.     }
  198.  
  199.  
  200.     /**
  201.      * Validate a specific field using the supplied plain validation array. This function converts
  202.      * the plain validation array into a structured validation array and then calls ValidationUtil::validateObject().
  203.      *
  204.      * @param objectType       The string object type
  205.      * @param object           The object to validate
  206.      * @param validationArray  The plain (numerically indexed) validation array
  207.      *
  208.      *  The expected structure for the validation array is as follows:
  209.      *  $validationArray[] = array ($fieldname, true/false, eq/neq/lt/lte/gt/gte/noop, $value, $errorMessage);
  210.      *
  211.      *  The noop value for the cmp_op field is only valid if the field is not required
  212.      *
  213.      * @return true/false value indicating whether the object validation passed or failed
  214.      */
  215.     function validateObjectPlain ($objectType$object$validationArray)
  216.     {
  217.         $validationControls array ();
  218.  
  219.         $vc array ();
  220.         foreach ($validationArray as $va{
  221.             $size count($va);
  222.             if ($size 5{
  223.                 return pn_exit ("ValidationUtil::validateObjectPlain : invalid validationArray supplied: expected 5 fields but found $size ... ");
  224.             }
  225.  
  226.             $vc['objectType'$objectType;
  227.             $vc['field']      $va[0];
  228.             $vc['required']   $va[1];
  229.             $vc['cmp_op']     $va[2];
  230.             $vc['cmp_value']  $va[3];
  231.             $vc['err_msg']    $va[4];
  232.  
  233.             $validationControls[$vc;
  234.         }
  235.  
  236.         return ValidationUtil::validateObject ($objectType$object$validationControls);
  237.     }
  238. }

Documentation generated on Fri, 18 Jul 2008 21:58:57 +0200 by phpDocumentor 1.4.1