title: form validation using oop php
body: <p>I want to validate a form using php by object oriented technique. I created a class validator by watching a tutorial but I want to add more rules in this class such as regular expressions and a number check and email check. Also the password must be eight to five characters in length with one being upper case and one lower case etc. But I can't add more rules in this class. </p>
  
  <blockquote>
    <p>I added a max length rule but it is not working.</p>
  </blockquote>
  
  <pre><code>&lt;?php 
  class Validator {
      // for form fields storing
      private $fields = array();
      //for storing errors for form fields
      private $field_errors = array();
       private $form_is_valid = true;
  
      public function add_field($field_name){
          $this-&gt;fields[]= $field_name;
          //associative array6
          $this-&gt;field_errors[$field_name] = array();
      }
      public function add_rule_to_field( $field_name, $field_rule){
          $rule_name = $field_rule[0];
  
  
  
          switch ($rule_name)
          {
              case 'min_length':
                  if (strlen($_POST[$field_name]) &lt; $field_rule[1]){
                      $this-&gt;add_error_to_field($field_name, ucwords($field_name). " cannot be less than {$field_rule[1]} in length");
                      //echo "must 1 chr &lt;br&gt;";
                  }
              break;
              case 'empty':
                  if(strlen($_POST[$field_name]) == 0){
                      $this-&gt;add_error_to_field($field_name, ucwords($field_name). " cannot be empty");
                      //echo "not empty &lt;br&gt;";
                  }
              break;
              case 'max_legnth':
                  if(strlen($_POST[$field_name]) &gt; $field_rule[2]){
                      $this-&gt;add_error_to_field($field_name, ucwords($field_name). " cannot be greater then {$field_rule[2]} in length");
                  }
              break;
              default:
              break;
          }
      }
      private function add_error_to_field($field_name, $error_message){
          $this-&gt;form_is_valid = false;
          $this-&gt;form_is_valid ;
          $this-&gt;field_errors[$field_name][] = $error_message;
          //echo "add error to filed is working &lt;br&gt;";
      }
      public function form_valid(){
          return $this-&gt;form_is_valid;
      }
      public function out_field_error($field_name){
          if (isset($this-&gt;field_errors[$field_name])){
              //echo "out ";
              foreach ($this-&gt;field_errors[$field_name] as $field_errors){
                  echo "&lt;p class='error'&gt; {$field_errors} &lt;/p&gt;";
                  //echo "out is running";
              }
          }
      }
  
  
  }
   ?&gt;
  </code></pre>
  
  <blockquote>
    <p>Here is my form and object instance of the class:</p>
  </blockquote>
  
  <pre><code>&lt;?php 
  
        require_once('validator.php');
        $validator = new Validator;
  
        if(isset($_POST['submit'])){
              $validator-&gt;add_field('name');
              $validator-&gt;add_rule_to_field('name', array('min_length', 5));
              $validator-&gt;add_rule_to_field('name', array('empty'));
  
              $validator-&gt;add_field('email');
              $validator-&gt;add_rule_to_field('email', array('min_length', 6));
              $validator-&gt;add_rule_to_field('email', array('empty'));
  
              $validator-&gt;add_field('number');
              $validator-&gt;add_rule_to_field('number', array('min_length', 8));
              $validator-&gt;add_rule_to_field('number', array('empty'));
              $validator-&gt;add_rule_to_field('number', array('max_length', 10));
  
              $validator-&gt;add_field('password');
              $validator-&gt;add_rule_to_field('password', array('min_length', 8));
              $validator-&gt;add_rule_to_field('password', array('empty'));
  
              $validator-&gt;filed_out();
  
  
              if ($validator-&gt;form_valid() == true){
                    echo " registration is sucessfull";
  
                    exit();
              }
              // else{
              //       echo "form is not valid";
              //      //exit();
              // }
  
  }
  
   ?&gt;
  
  
  
  
  
  &lt;form method="POST" id="form-add" action=""&gt;
              &lt;label&gt;Name:&lt;/label&gt;
              &lt;input type="text" name="name" value=""/&gt;
              &lt;span&gt; &lt;?php $validator-&gt;out_field_error('name'); ?&gt;&lt;/span&gt;
  
              &lt;label&gt;Email:&lt;/label&gt;
              &lt;input type="text" name="email" value=""/&gt;
              &lt;span&gt;&lt;?php $validator-&gt;out_field_error('email'); ?&gt;&lt;/span&gt;
  
              &lt;label&gt;Number:&lt;/label&gt;
              &lt;input type="text" name="number" value=""/&gt;
              &lt;span&gt;&lt;?php $validator-&gt;out_field_error('number'); ?&gt;&lt;/span&gt;
  
              &lt;label&gt;Password:&lt;/label&gt;
              &lt;input type="text" name="password" value=""/&gt;
              &lt;span&gt;&lt;?php $validator-&gt;out_field_error('password'); ?&gt;&lt;/span&gt;
  
              &lt;label&gt;Re Enter Password:&lt;/label&gt;
              &lt;input type="text" name="cpassword" value=""/&gt;
              &lt;span&gt;&lt;/span&gt;
  
              &lt;input type="submit" name="submit" value="SUBMIT"/&gt;
          &lt;/form&gt;
  </code></pre>