Public paste
form-validator
By: icro | Date: Nov 19 2009 08:50 | Format: PHP | Expires: never | Size: 25.03 KB | Hits: 1021

  1. <?php
  2.  
  3. class FORMVALIDATOR
  4. {
  5.  
  6.       // speichert die Validierungsregeln
  7.       protected $rules  = array();
  8.            
  9.       // speichert die POST-Werte
  10.       protected $value  = array();
  11.      
  12.       // speichert die Fehler
  13.       protected $errors = array();
  14.      
  15.       // Kopie des Input-Arrays
  16.       protected $input  = array();
  17.      
  18.       // Kopie des Sprach-Arrays
  19.       protected $lang   = array();
  20.      
  21.      
  22.      
  23.       /**
  24.        * param array config
  25.        */
  26.       public function __construct
  27.       (
  28.         array $config = array(
  29.                               'input' => array('key' => 'value'),
  30.                               'lang' => array('key' => 'value')
  31.                              )
  32.       )
  33.       {      
  34.               if(count($config['input']) > 0) $this->input = $config['input'];              
  35.               if(count($config['lang'])  > 0) $this->lang = $config['lang'];
  36.       }
  37.      
  38.      
  39.      
  40.       /**
  41.        * param $options array siehe values
  42.        */
  43.       public function register
  44.       (
  45.         array $options = array(
  46.                                'name'      => 'str: formfieldname',
  47.                                'validate'  => 'str: regexp-pattern-case',
  48.                                'min'       => 'int: minlength',
  49.                                'max'       => 'int: maxlength',
  50.                                'mustmatch' => 'str: related-to-formfieldname',
  51.                                'optional'  => 'bool: field can be empty',
  52.                                'callback'  => 'str: callback-method',
  53.                                'regexp'    => 'str: regexp (related to $option:validate)'
  54.                               )
  55.       )
  56.       {
  57.      
  58.               // $options prfen:      
  59.               if(count($options) > 0)
  60.               {
  61.                 foreach($options as $k => $v) if(empty($v)) unset($options[$k]);
  62.                 if(!count($options) > 0) return false;
  63.                 if(!array_key_exists('name', $options)) return false;
  64.               }
  65.               else
  66.               {
  67.                 return false;
  68.               }
  69.              
  70.            
  71.            
  72.               // parse key validate
  73.               if(isset($options['validate']))
  74.               {
  75.  
  76.                 switch($options['validate'])
  77.                 {
  78.  
  79.                       case 'alpha':
  80.                           $rules['regexp'] = "|^[D ']*$|";
  81.                           break;
  82.                          
  83.                       case 'alpha_dash_underscore':
  84.                           $rules['regexp'] = "|^[a-zA-Z-_]*$|";
  85.                           break;
  86.                          
  87.                       case 'alpha_dash_underscore_dot':
  88.                           $rules['regexp'] = "|^[a-zA-Z-_.]*$|";
  89.                           break;
  90.                          
  91.                       case 'alphastring':
  92.                           $rules['regexp'] = "|^[D '-.]*$|";
  93.                           break;
  94.  
  95.                       case 'alphanumeric':
  96.                           $rules['regexp'] = '|^[a-zA-Z0-9]*$|';
  97.                           break;
  98.  
  99.                       case 'numeric':
  100.                           $rules['regexp'] = '|^[0-9]*$|';
  101.                           break;
  102.  
  103.                       case 'email':
  104.                           $rules['regexp'] = '#^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$#';
  105.                           break;
  106.  
  107.                       case 'captcha':
  108.                           $rules['callback'] = 'this:checkCaptcha';
  109.                           break;
  110.  
  111.                 }
  112.               }
  113.               if(isset($options['callback']))  $rules['callback']  = $options['callback'];
  114.               if(isset($options['regexp']))    $rules['regexp']    = $options['regexp'];
  115.               if(isset($options['mustmatch'])) $rules['mustmatch'] = $options['mustmatch'];
  116.               if(isset($options['optional']))  $rules['optional']  = $options['optional'];
  117.               if(isset($options['min']))       $rules['min']       = $options['min'];
  118.               if(isset($options['max']))       $rules['max']       = $options['max'];
  119.               if(isset($options['validate']))  $rules['validate']  = $options['validate'];
  120.              
  121.              
  122.              
  123.              
  124.               // Regeln fr das aktuelle Feld speichern
  125.               $this->rules[$options['name']] = $rules;
  126.              
  127.               // Post-Value fr das aktuelle Feld speichern
  128.               $this->value[$options['name']] = $this->input[$options['name']];
  129.              
  130.       }
  131.      
  132.      
  133.      
  134.       /**
  135.        * param formfieldname_prefix string prefix, welches ihr an den namen des Formfields setzen knnt
  136.        * param languagecache_prefix string prefix, welches ihr an den key im Language-Cache setzen knnt
  137.        * return int count(errors)
  138.        */
  139.       public function validate
  140.       (
  141.         $formfieldname_prefix = '',
  142.         $languagecache_prefix = ''
  143.       )
  144.       {
  145.      
  146.               // Sind Regeln und Values ein Array,
  147.               if(is_array($this->rules) && is_array($this->value))
  148.               {
  149.                 // durchlaufe die Post-Values.
  150.                 foreach($this->value as $k => $v)
  151.                 {
  152.                
  153.                   // Existiert der Feldname in den Regeln,
  154.                   if(array_key_exists($k, $this->rules))
  155.                   {
  156.                     // ist der Feldname optional,
  157.                     if(isset($this->rules[$k]['optional']) && $this->rules[$k]['optional'] === true)
  158.                     {
  159.                       // und ist das Feld leer, bedarf es keiner weiteren berprfung... also weiter;
  160.                       if(empty($v)) continue;
  161.                     }
  162.                    
  163.                     // ansonsten,
  164.                    
  165.                    
  166.                    
  167.                    
  168.                     ##########################################################################################
  169.                    #  PRIMARY:
  170.                    #  Prfe, ob das Feld leer sein darf.
  171.                    #  Alle Felder sind grundstzlich Pflichtfelder, es sei denn,
  172.                    #    'optional' existiert und steht auf 'true'.
  173.                    ##########################################################################################
  174.                    if((!isset($this->rules[$k]['optional'])) || (isset($this->rules[$k]['optional']) && $this->rules[$k]['optional'] === false))
  175.                     {
  176.                       // ist er leer,
  177.                       if(empty($v))
  178.                       {
  179.                         // und exisitiert ein mustmatchfeld und ist dieses bereits eine Fehlermeldung,
  180.                         if(isset($this->rules[$k]['mustmatch']) && isset($this->errors[$this->rules[$k]['mustmatch']]))
  181.                         {
  182.                           // gehe weiter, um nicht fr alle mustmatch-Felder Folgefehler auszugeben;
  183.                           continue;
  184.                         }
  185.                         else
  186.                         {
  187.                           // ansonsten, schreibe den Fehler
  188.                           $this->errors[$k] = sprintf($this->lang[$languagecache_prefix.'error_emptyfield'],
  189.                                                       // und schreibe den Labelnamen fr dieses Feld in den Fehler
  190.                                                       $this->lang[str_replace($formfieldname_prefix, $languagecache_prefix, $k)]);
  191.                           // und gehe weiter, um diesen Fehler nicht mit Folgefehlern zu berschreiben.
  192.                           continue;                          
  193.                         }
  194.                       }
  195.                     }
  196.                    
  197.                    
  198.                    
  199.                    
  200.                     ##########################################################################################
  201.                    #  SECONDARY:
  202.                    #  Prfe, ob das Feld eine RegExp hat und ob diese true auf den preg_match zurckgibt.
  203.                    ##########################################################################################
  204.                    if(isset($this->rules[$k]['regexp']) && !preg_match($this->rules[$k]['regexp'], $v))
  205.                     {
  206.                         // exisitiert ein mustmatchfeld und ist dieses bereits eine Fehlermeldung,
  207.                         if(isset($this->rules[$k]['mustmatch']) && isset($this->errors[$this->rules[$k]['mustmatch']]))
  208.                         {
  209.                           // gehe weiter, um nicht fr alle mustmatch-Felder Folgefehler auszugeben;
  210.                           continue;
  211.                         }
  212.                         else
  213.                         {
  214.                           // ansonsten, schreibe den Standard-Fehler...
  215.                           $this->errors[$k] = sprintf($this->lang[$languagecache_prefix.'error_invalid_data'],
  216.                                                         // und schreibe den Labelnamen fr dieses Feld in den Fehler
  217.                                                         $this->lang[str_replace($formfieldname_prefix, $languagecache_prefix, $k)])
  218.                                                       .' '.
  219.                                                       // ...gefolgt von dem RegExp-spezifischen Hinweis, was hier eigentlich rein darf
  220.                                                       $this->lang[$languagecache_prefix.'error_validate_'.$this->rules[$k]['validate']]
  221.                                                       ;
  222.                           // und gehe weiter, um diesen Fehler nicht mit Folgefehlern zu berschreiben.
  223.                           continue;                          
  224.                         }
  225.                     }
  226.                    
  227.                    
  228.                    
  229.                    
  230.                     ##########################################################################################
  231.                    #  TERTIARY:
  232.                    #  Prfe, ob der Feldwert der Mindestlnge entspricht.
  233.                    ##########################################################################################
  234.                    if(isset($this->rules[$k]['min']) && $this->rules[$k]['min'] > 0)
  235.                     {
  236.                       // ist der Feldwert kleiner als die Mindestlnge,
  237.                       if(strlen($v) < $this->rules[$k]['min'] && !empty($v))
  238.                       {
  239.                         // exisitiert ein mustmatchfeld und ist dieses bereits eine Fehlermeldung,
  240.                         if(isset($this->rules[$k]['mustmatch']) && isset($this->errors[$this->rules[$k]['mustmatch']]))
  241.                         {
  242.                           // gehe weiter, um nicht fr alle mustmatch-Felder Folgefehler auszugeben;
  243.                           continue;
  244.                         }
  245.                         else
  246.                         {
  247.                           // ansonsten, schreibe den Fehler
  248.                           $this->errors[$k] = sprintf($this->lang[$languagecache_prefix.'error_length_min'],
  249.                                               // und schreibe den Minimalwert fr dieses Feld in den Fehler
  250.                                               $this->lang[str_replace($formfieldname_prefix, $languagecache_prefix, $k)], $this->rules[$k]['min']);
  251.                           // und gehe weiter, um diesen Fehler nicht mit Folgefehlern zu berschreiben.
  252.                           continue;                          
  253.                         }
  254.                       }
  255.                     }
  256.                    
  257.                    
  258.                    
  259.                    
  260.                     ##########################################################################################
  261.                    #  QUARTARY:
  262.                    #  Prfe, ob der Feldwert die Maximallnge einhlt.
  263.                    ##########################################################################################
  264.                    if(isset($this->rules[$k]['max']) && $this->rules[$k]['max'] > 0)
  265.                     {
  266.                       // ist der Feldwert grer als die Maximallnge,
  267.                       if(strlen($v) > $this->rules[$k]['max'] && !empty($v))
  268.                       {
  269.                         // exisitiert ein mustmatchfeld und ist dieses bereits eine Fehlermeldung,
  270.                         if(isset($this->rules[$k]['mustmatch']) && isset($this->errors[$this->rules[$k]['mustmatch']]))
  271.                         {
  272.                           // gehe weiter, um nicht fr alle mustmatch-Felder Folgefehler auszugeben;
  273.                           continue;
  274.                         }
  275.                         else
  276.                         {
  277.                           // ansonsten, schreibe den Fehler
  278.                           $this->errors[$k] = sprintf($this->lang[$languagecache_prefix.'error_length_max'],
  279.                                                       // und schreibe den Maximalwert fr dieses Feld in den Fehler
  280.                                                       $this->lang[str_replace($formfieldname_prefix, $languagecache_prefix, $k)], $this->rules[$k]['max']);
  281.                           // und gehe weiter, um diesen Fehler nicht mit Folgefehlern zu berschreiben.
  282.                           continue;                          
  283.                         }
  284.                       }
  285.                     }
  286.                    
  287.                    
  288.                    
  289.                    
  290.                     ##########################################################################################
  291.                    #  QUINARY:
  292.                    #  Prfe, ob der Feldwert die den Wert des Mustmatch-Feldes enthlt.
  293.                    ##########################################################################################
  294.                    if(isset($this->rules[$k]['mustmatch']) && isset($this->value[$this->rules[$k]['mustmatch']]))
  295.                     {
  296.                       // stimmen die Werte nicht berein,
  297.                       if($v != $this->value[$this->rules[$k]['mustmatch']])
  298.                       {
  299.                         // schreibe den Fehler,
  300.                         $this->errors[$k] = sprintf($this->lang[$languagecache_prefix.'error_mustmatch'],
  301.                                                     // schreibe den Labelnamen dieses Feldes in den Fehler
  302.                                                     $this->lang[str_replace($formfieldname_prefix, $languagecache_prefix, $k)],
  303.                                                     // und den Labelnamen fr das Feld,
  304.                                                     // mit dem die Werte dieses Feldes bereinstimmen sollten
  305.                                                     $this->lang[str_replace($formfieldname_prefix, $languagecache_prefix, $this->rules[$k]['mustmatch'])]);
  306.                         // und gehe weiter, um diesen Fehler nicht mit Folgefehlern zu berschreiben.
  307.                         continue;
  308.                       }
  309.                     }
  310.                    
  311.                    
  312.                    
  313.                    
  314.                     ##########################################################################################
  315.                    #  SENARY:
  316.                    #  Prfe, ob der Feldwert ein Callback enthlt.
  317.                    ##########################################################################################                    
  318.                    if (isset($this->rules[$k]['callback']))
  319.                     {
  320.                       $callback = $this->rules[$k]['callback'];
  321.                       if (substr($callback, 0, 5) == 'this:')
  322.                       {
  323.                         // Methoden-Name
  324.                         $method = substr($callback, 5);
  325.                         // prfe, ob diese Klasse die geforderte Methode enthlt
  326.                         if(method_exists($this, $method))
  327.                         {
  328.                           // gibt das Callback false zurck
  329.                           if(!$this->$method($v))
  330.                           {
  331.                             // exisitiert ein mustmatchfeld und ist dieses bereits eine Fehlermeldung,
  332.                             if(isset($this->rules[$k]['mustmatch']) && isset($this->errors[$this->rules[$k]['mustmatch']]))
  333.                             {
  334.                               // gehe weiter, um nicht fr alle mustmatch-Felder Folgefehler auszugeben;
  335.                               continue;
  336.                             }
  337.                             else
  338.                             {
  339.                               // ansonsten, schreibe den Fehler
  340.                               $this->errors[$k] = $this->lang[$languagecache_prefix.'error_callback_'.$method];
  341.                               continue;                          
  342.                             }
  343.                           }
  344.                           else
  345.                           {
  346.                             // echo 'iwas, was passieren knnte, wenn das callback true zurckgibt';
  347.                           }
  348.                         }
  349.                         else
  350.                         {
  351.                           // vertippt?
  352.                           $this->errors[$k] = 'missing callback '.$method;
  353.                           continue;                          
  354.                         }
  355.                       }
  356.                     }
  357.                    
  358.                      
  359.                      
  360.                      
  361.                   }
  362.                 }
  363.               }
  364.              
  365.              
  366.              
  367.              
  368.               // gib die Summe der Fehler zurck
  369.               return count($this->errors);
  370.              
  371.       }
  372.      
  373.      
  374.      
  375.       /**
  376.        * return arary errors
  377.        */
  378.       public function errors()
  379.       {
  380.      
  381.               return $this->errors;
  382.              
  383.       }
  384.      
  385.      
  386.      
  387.       /**
  388.        * callback zum testen
  389.        *
  390.        * return bool
  391.        */
  392.       public function testCallbackEmail($email_address)
  393.       {
  394.      
  395.               if
  396.               (preg_match
  397.                  (
  398.                    '#^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$#',
  399.                    $email_address
  400.                  )
  401.               )
  402.               {
  403.                 return true;
  404.               }
  405.               else
  406.               {
  407.                 return false;
  408.               }      
  409.              
  410.       }
  411.      
  412.      
  413. } // end class
  414.  
  415.  
  416.  
  417.  
  418. ##############################################################################################
  419. # DUMMIES
  420. ##############################################################################################
  421.  
  422. /**
  423.  * Konstruiert ein Dummy-Object, in welchem die Sprachvariablen fr multilingualen Support gespeichert werden.
  424.  */
  425. class LANGUAGE
  426. {
  427.       public function __construct()
  428.       {
  429.      
  430.               // Dummy-Array fr Sprachvariablen              
  431.               $this->cache = array(// label-Namen der Formularfelder
  432.                                    'global_text_contactform_name' => "user name",
  433.                                    'global_text_contactform_email' => "email address",
  434.                                    'global_text_contactform_email_retype' => "re-type email address",
  435.                                    'global_text_contactform_password' => "password",
  436.                                    'global_text_contactform_password_retype' => "re-type password",
  437.                                    'global_text_contactform_birthday_year' => "year",
  438.                                    // Standard-fehler
  439.                                    'global_text_contactform_error_emptyfield' => "The field <em>%1$s</em> is empty.",
  440.                                    'global_text_contactform_error_length_minmax' => "The field <em>%1$s</em> must be between %2$d and %3$d characters in length.",
  441.                                    'global_text_contactform_error_length_min' => "The field <em>%1$s</em> must be %2$d characters in minimum length.",
  442.                                    'global_text_contactform_error_length_max' => "The field <em>%1$s</em> must be %2$d characters in maximum length.",
  443.                                    'global_text_contactform_error_mustmatch' => "The field <em>%1$s</em> do not match with <em>%2$s</em>. Please retype it in the specified box.",
  444.                                    // regexp-Fehler
  445.                                    'global_text_contactform_error_validate_email' => "Your email address is invalid. Please type it again making sure the spelling is correct.",
  446.                                    'global_text_contactform_error_validate_alpha' => "Allowed are only letters.",
  447.                                    'global_text_contactform_error_validate_alpha_dash_underscore' => "Allowed are only a-z, A-Z, dash and underscore.",
  448.                                    'global_text_contactform_error_validate_alpha_dash_underscore_dot' => "Allowed are only a-z, A-Z, dash, underscore and dot.",
  449.                                    'global_text_contactform_error_validate_alpha_alphastring' => "Allowed are only letters, dash, dot and tick mark.",
  450.                                    'global_text_contactform_error_validate_alphanumeric' => "Allowed are only a-z, A-Z and 0-9.",
  451.                                    'global_text_contactform_error_validate_numeric' => "Allowed are only numbers.",
  452.                                    // Callback-Fehler
  453.                                    'global_text_contactform_error_callback_testCallbackEmail' => "Generated by your Method testCallbackEmail(): Email-Address in incorrect.",
  454.                                   );
  455.                                  
  456.       }
  457. }
  458.                          
  459.  
  460. /**
  461.  * Konstruiert ein Dummy-Object, in welchem die POST-Daten gespeichert werden.
  462.  */
  463. class CORE
  464. {
  465.       public function __construct()
  466.       {
  467.      
  468.               // Dummy-Array an Stelle eingehender POST-Daten
  469.               $this->input = array('author_name' => 'i',
  470.                                    'author_email' => 'icronosonic@hotmail.com',
  471.                                    'author_email_retype' => 'icronosonic@hotmail.com',
  472.                                    'author_password' => '1234567890',
  473.                                    'author_password_retype' => '1234567890',
  474.                                    'author_birthday_year' => '1900');
  475.                                    
  476.       }
  477. }
  478.  
  479.  
  480.  
  481.  
  482. ##############################################################################################
  483. # DUMMIES INITIALISIEREN
  484. ##############################################################################################
  485.  
  486. $CORE = new CORE;
  487. $LANGUAGE = new LANGUAGE;
  488.  
  489.  
  490.  
  491.  
  492. ##############################################################################################
  493. # TEST
  494. ##############################################################################################
  495.  
  496. /**
  497.  * bergabe der POST und Sprach-Variablen an den Constructor
  498.  */
  499. $FFV = new FORMVALIDATOR(array('input' => $CORE->input, 'lang' => $LANGUAGE->cache), 'author_');
  500.  
  501. /**
  502.  * Registrierung der Formfelder und bergabe der Validierungs-Parameter
  503.  */
  504. $FFV->register(array('name' => 'author_name', 'validate' => 'alpha_dash_underscore', 'min' => 2, 'max' => 22));
  505. $FFV->register(array('name' => 'author_password', 'min' => 8, 'max' => 32));
  506. $FFV->register(array('name' => 'author_email', 'validate' => 'email'));
  507. $FFV->register(array('name' => 'author_email_retype', 'validate' => 'email', 'mustmatch' => 'author_email'));
  508. $FFV->register(array('name' => 'author_password_retype', 'mustmatch' => 'author_password', 'min' => 8, 'max' => 32));
  509. $FFV->register(array('name' => 'author_birthday_year', 'validate' => 'numeric', 'optional' => true, 'min' => 4, 'max' => 4));
  510.  
  511. /**
  512.  * POST-Daten Validieren
  513.  */
  514. if($FFV->validate('author_', 'global_text_contactform_') > 0) $test = $FFV->errors();
  515.  
  516.  
  517. ##############################################################################################
  518. # CALLBACK-TEST
  519. ##############################################################################################
  520.  
  521. /**
  522.  * bergabe der POST und Sprach-Variablen an den Constructor
  523.  */
  524. $FFV = new FORMVALIDATOR(array('input' => $CORE->input, 'lang' => $LANGUAGE->cache), 'author_');
  525.  
  526. /**
  527.  * Registrierung des Feldes authorname als Email-Validierung im Callback
  528.  */
  529. $FFV->register(array('name' => 'author_name', 'callback' => 'this:testCallbackEmail'));
  530.  
  531. /**
  532.  * POST-Daten Validieren
  533.  */
  534. if($FFV->validate('author_', 'global_text_contactform_') > 0) $callbackTest = $FFV->errors();
  535.  
  536.  
  537.  
  538.  
  539. ##############################################################################################
  540. # FEHLERAUSGABE
  541. ##############################################################################################
  542.  
  543.  
  544. echo "<pre>";
  545.  
  546.   print_r($test);
  547.   print_r($callbackTest);
  548.  
  549. echo "</pre>";