Guest User

fuelphp validation rules

a guest
Sep 4th, 2013
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.77 KB | None | 0 0
  1. <?php
  2.  
  3. class ValidateRules
  4. {
  5.    
  6.     static private $folderName = 'rules';
  7.    
  8.    
  9.     /**
  10.      * get external validation rules and instance of Validation() object
  11.      *  
  12.      * @name name of file rules or send array of validate rules
  13.      * @name instanceName name of instance Validatio::forge()
  14.      *
  15.      * @return instance of Validation::forge()
  16.      */
  17.     public static function getRules($name, $instanceName = null)
  18.     {
  19.         if($instanceName)
  20.         {
  21.             $instance = \Validation::forge($instanceName);
  22.         } else {
  23.             $instance = \Validation::forge(\Str::random());
  24.         }
  25.        
  26.         $fileName = explode('_', $name);
  27.        
  28.         $file = APPPATH. DS .$fileName[0]. DS .self::$folderName. DS .$name.'.php';
  29.        
  30.         $rules = include($file);
  31.        
  32.         $debug = '';
  33.        
  34.         foreach($rules as $field => $item)
  35.         {
  36.             $instance->add_field( $field, $item['label'], self::convertToString($item['rules']) );
  37.         }
  38.        
  39.         return $instance;
  40.     }
  41.    
  42.    
  43.     /**
  44.      *
  45.      * @return string
  46.      */
  47.     private static function convertToString($rules = array())
  48.     {
  49.         $string = '';
  50.         foreach($rules as $name => $rule)
  51.         {
  52.            
  53.             if(is_array($rule))
  54.             {
  55.                 $string .= $name.'[';
  56.                 $string .= implode(',', $rule);
  57.                 $string .= ']|';
  58.             }
  59.             elseif($name == $rule)
  60.             {
  61.                 $string .= $rule . '|';
  62.             } else {
  63.                 $string .= $name.'['.$rule.']|';
  64.             }
  65.            
  66.         }
  67.        
  68.         $string = trim(rtrim($string, '|'));
  69.        
  70.         return $string;
  71.     }
  72.    
  73.    
  74. }
Advertisement
Add Comment
Please, Sign In to add comment