Advertisement
Guest User

Untitled

a guest
Sep 24th, 2017
488
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.15 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <title>form validator</title>
  5. </head>
  6. <body>
  7. <?php
  8. global $formData;
  9. $requestMethod = $_SERVER['REQUEST_METHOD'];
  10.  
  11. class formValidator {
  12.     public $empty;
  13.  
  14.     private $currentRules;
  15.     public  $fieldName;
  16.     public  $fieldValue;
  17.     private $formFieldName;
  18.     private $formFieldValue;
  19.     private $badVars;
  20.     private $countOfBadVars;
  21.     private static $errorReport;
  22.     private $errorsCount;
  23.  
  24.     function __construct(){
  25.         $this->activateListOfBadVars();
  26.     }
  27.  
  28.     public function acceptRules($rules){
  29.         $this->currentRules = $rules;
  30.  
  31.         if(!empty($this->currentRules) || !empty($rules)){
  32.             $this->verifyFields();
  33.         } else {
  34.             $this->prepareErrorReport('there no rules to check fields with');
  35.         }
  36.  
  37.     }
  38.  
  39.     public function prepareErrorReport($errorMessage, $fromCase='', $dataType=''){
  40.         self::$errorReport[] = $errorMessage . "<br />";
  41.         $this->errorsCount = count(self::$errorReport);
  42.  
  43.     }
  44.  
  45.     protected function activateListOfBadVars(){
  46.         $variableList[] = 'submit_button';
  47.         $this->countOfBadVars = count($variableList);
  48.         $this->badVars = $variableList;
  49.  
  50.         for ($i = 0; $i < ($this->countOfBadVars); $i++){
  51.             unset($_POST[$this->badVars[$i]]);
  52.         }
  53.  
  54.     }
  55.  
  56.     private function trimmer($field, $value){
  57.         $this->formFieldName[] = $this->fieldName  = strip_tags(trim($field));
  58.         $this->formFieldValue[] = $this->fieldValue = strip_tags(trim($value));
  59.  
  60.     }
  61.  
  62.     private function inputChecker($pattern, $dataType, $errorMessage){
  63.         if (!empty($this->fieldValue)) {
  64.             $currentCountOfErrors = 0;
  65.  
  66.             foreach ($pattern as $currentPattern) {
  67.                 if (substr_count(($this->fieldValue), $currentPattern) > 0) {
  68.                     ++$currentCountOfErrors;
  69.                 }
  70.             }
  71.            
  72.             if ($currentCountOfErrors > 0) {
  73.                 $this->prepareErrorReport($errorMessage . " in $dataType field ", 'onlychars', $dataType);
  74.             }
  75.         }
  76.        
  77.     }
  78.  
  79.     private function verifyFields(){
  80.         foreach ($_POST as $POSTfieldName => $POSTfieldValue) {
  81.             $this->trimmer($POSTfieldName, $POSTfieldValue);
  82.             $dataType = str_replace('_field', '', $this->fieldName);
  83.  
  84.             if (array_key_exists($dataType, $this->currentRules)) {
  85.                 foreach($this->currentRules[$dataType] as $currentRule => $errorMessage) {
  86.                     switch($currentRule){
  87.                         case 'required':
  88.                             if (empty($this->fieldValue)) {
  89.                             $this->prepareErrorReport($errorMessage, 'required', $dataType);
  90.                             }
  91.                         break;
  92.  
  93.                         case 'maxlength':
  94.                             if (strlen($this->fieldValue) > 32) {
  95.                             $this->prepareErrorReport($errorMessage, 'maxlength', $dataType);
  96.                         }
  97.                         break;
  98.                        
  99.                         case 'onlychars':
  100.                             $pattern = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 0, '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+', '-', '=', '|', '?', '±', '~', '`', "'", '[', ']', ';', '\\', '/', '<', ',', ".");
  101.                             $this->inputChecker($pattern, $dataType, $errorMessage);
  102.                         break;
  103.  
  104.                         case 'onlynumbers':
  105.                             $pattern = array('q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'z', 'x', 'c', 'v', 'b', 'n',  'm', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '=', '|', '?', '±', '~', '`', "'", '[', ']', ';', '\\', '/', '<', ',', ".");
  106.                             $this->inputChecker($pattern, $dataType, $errorMessage);
  107.                         break;
  108.  
  109.                         case 'specchar':
  110.                             $pattern = array('!', '#', '$', '%', '^', '&', '*', '(', ')', '_', '=', '|', '?', '±', '~', '`', "'", '[', ']', ';', '\\', '/', '<', ',');
  111.  
  112.                             if (!empty($this->fieldValue)) {
  113.                             $currentCountOfErrors = 0;
  114.                             foreach ($pattern as $currentPattern) {
  115.                                 if ((substr_count(($this->fieldValue), $currentPattern) > 0) || (substr_count(($this->fieldValue), '@') > 1) || (substr_count(($this->fieldValue), '@') <= 0) || (substr_count(($this->fieldValue), '.') < 1)) {
  116.                                     ++$currentCountOfErrors;
  117.                                 }
  118.                             }
  119.  
  120.                                 if ($currentCountOfErrors > 0) {
  121.                                     $this->prepareErrorReport($errorMessage . " in $dataType field ", 'onlychars', $dataType);
  122.                                 }
  123.                         }
  124.                         break;
  125.                     }
  126.                 }
  127.  
  128.             }
  129.         }
  130.  
  131.     if(!in_array($_POST['sex_listbox'], array('male', 'female', 'select your sex'))) {
  132.         $this->prepareErrorReport('there something wrong with your sex');
  133.     }
  134.  
  135.     if (in_array($_POST['sex_listbox'], array('select your sex'))){
  136.         $this->prepareErrorReport('select your sex');
  137.     }
  138.  
  139.     }
  140.  
  141.     public function displayAllErrors(){
  142.         for ($i = 0; $i < ($this->errorsCount); $i++){
  143.             echo self::$errorReport[$i];
  144.         }
  145.  
  146.     }
  147.  
  148.     public function takeOutSomeVars(){
  149.         global $formData;
  150.         $formKey = $this->formFieldName;
  151.         $formValue = $this->formFieldValue;
  152.         $formData = array_combine($formKey, $formValue);
  153.  
  154.     }
  155.  
  156. }
  157.  
  158. if ($requestMethod == 'POST') {
  159.     $emptyRules = '';
  160.     $rules = array(
  161.         'name' => array(
  162.             'required'   => 'name is required field',
  163.             'maxlength'  => 'max length must be 32',
  164.             'onlychars'  => 'only characters [A-Z][a-z] are available to input'
  165.         ),
  166.         'surname' => array(
  167.             'required'   => 'surname is required field',
  168.             'maxlength'  => 'max length must be 32',
  169.             'onlychars'  => 'only characters [A-Z][a-z] are available to input'
  170.         ),
  171.         'email' => array(
  172.             'required'   => 'email is required field',
  173.             'maxlength'  => 'max length must be 32',
  174.             'specchar'   => 'there problems with your entered email (be sure you entered it like: info@site.com)'
  175.         ),
  176.         'telephone' => array(
  177.             'required'    => 'telephone is required field',
  178.             'maxlength'   => 'max length must be 12',
  179.             'onlynumbers' => 'only [+, -] sign and numbers [0-9] are available to input'
  180.         )
  181.     );
  182.  
  183.     $newValidator = new formValidator();
  184.     $newValidator->acceptRules($rules);
  185.     $newValidator->displayAllErrors();
  186.     $newValidator->takeOutSomeVars();
  187.  
  188. }
  189.  
  190. #EMPTY
  191.  
  192. ?>
  193. <form action="" method="POST" name="validator_form">
  194.     <table name="validator_table">
  195.         <tr>
  196.             <td>
  197.                 <input type="text" name="name_field" maxlength="32" value="<?php echo !empty($formData['name_field']) ? $formData['name_field'] : ''; ?>" />
  198.             </td>
  199.             <td>
  200.                 : Name field
  201.             </td>
  202.         </tr>
  203.         <tr>
  204.             <td>
  205.                 <input type="text" name="surname_field" maxlength="32" value="<?php echo !empty($formData['surname_field']) ? $formData['surname_field'] : ''; ?>" />
  206.             <td>
  207.                 : Surname field
  208.             </td>
  209.         </tr>
  210.         <tr>
  211.             <td>
  212.                 <input type="text" name="email_field" maxlength="32" value="<?php echo !empty($formData['email_field']) ? $formData['email_field'] : ''; ?>" />
  213.             </td>
  214.             <td>
  215.                 : E-mail field
  216.             </td>
  217.         </tr>
  218.         <tr>
  219.             <td>
  220.                 <input type="text" name="telephone_field" maxlength="12" value="<?php echo !empty($formData['telephone_field']) ? $formData['telephone_field'] : ''; ?>" />
  221.             </td>
  222.             <td>
  223.                 : Telephone field
  224.             </td>
  225.         </tr>
  226.         <tr>
  227.             <td>
  228.             <select name="sex_listbox">
  229.                 <option value="select your sex" <?php echo isset($formData['sex_listbox']) && $formData['sex_listbox'] == 'select your sex'    ? 'selected' : '' ?>>select your sex</option>
  230.                 <option value="male"   <?php echo isset($formData['sex_listbox']) && $formData['sex_listbox'] == 'male'   ? 'selected' : '' ?>>male</option>
  231.                 <option value="female" <?php echo isset($formData['sex_listbox']) && $formData['sex_listbox'] == 'female' ? 'selected' : '' ?>>female</option>
  232.                 <option value="ufo"    <?php echo isset($formData['sex_listbox']) && $formData['sex_listbox'] == 'ufo'    ? 'selected' : '' ?>>ufo</option>
  233.             </select>
  234.             </td>
  235.         </tr>
  236.         <tr>
  237.             <td>
  238.                 <input type="submit" name="submit_button" value="submit" />
  239.             </td>
  240.         </tr>
  241.     </table>
  242. </form>
  243. </body>
  244. </html>
  245. <?php
  246.  
  247. # EMPTY
  248.  
  249. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement