1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

error in Validation class / check method (for HTML form)

Discussion in 'PHP' started by iago111, Aug 29, 2020.

  1. #1
    Hello,
    I have a Validation class here, where I validate user input from a HTML form.

    I have an array of presettings that I pass to a check method of the Validation class. I first of all check if the input fields are empty or not, also checking there with the help of the presettings array if it is required that the field must not be empty. If an field is empty, I pass an error-message to the an error-Array. If no field is empty I give out: "Passed"

    That check method works fine, the only thing is when the first input field "username" is not empty, it doesn't check the other fields anymore and gives out "passed" immediately.

    Does anybody see the error here? Thank's a lot!

    register.php:

    require_once 'core/init.php';
    
    if(Input::exists()) {
      
       $validate = new Validate();
        $validate->check($_POST,array(
    
            'username' => array(
    
                'required' => true,
                'min' => 2,
                'max' => 20,
                'unique' => 'users'
            ),
            'password' => array(
                'required' => true,
                'min' => 6
    
            ),
            'password_again' => array(
                'required' => true,
                'matches' => 'password'
    
            ),
            'name' => array(
                'required' => true,
                'min' => 2,
                'max' => 50
            )
       ));
    
       if($validate->passed()) {
    
            echo 'Passed';
      
        } else {
    
            print_r($validate->errors());
        }
    }
    
    ?>
    
    <form action="" method="POST">
        <div class="field">
            <label for="username">Username</label>
            <input type="text" name="username" id="username"/>
    
        </div>
        <div class="field">
            <label for="password">Choose a password</label>
            <input type="password" name="password" id="password"/>
        </div>
         <div class="field">
            <label for="password_again">Enter your password again</label>
            <input type="password" name="password_again" id="password_again"/>
        </div>
         <div class="field">
            <label for="name">Enter your Name</label>
            <input type="text" name="name" id="name"/>
        </div>
            <input type="submit" value="Register"/>
    
    </form>
    PHP:
    Validate.php:

    class Validate {
    
        private $_passed = false,
                $_errors = array(),
                $_db = null;
    
        public function __construct() {
    
            $this->_db = DB::getInstance();
        }  
       
        public function check($source, $items = []) {
    
            foreach($items as $item => $rules) {
    
                foreach($rules as $rule => $rule_value) {
                   
                    $value = $source[$item];
    
                    if($rule === 'required' && empty($value)) {
    
                       $this->addError("{$item} is required");
    
                    //  echo $item." = ".$value."<br/>";
                    }
    
                }
    
                if(empty($this->_errors)) {
    
                    $this->_passed = true;
                }
    
            }
    
        }
        private function addError($error) {
            $this->_errors[] = $error;
    
        }
    
        public function errors() {
            return $this->_errors;
        }
    
    
        public function passed() {
    
            return $this->_passed;
    
    
        }
    }
    PHP:
     
    iago111, Aug 29, 2020 IP
  2. iago111

    iago111 Member

    Messages:
    99
    Likes Received:
    4
    Best Answers:
    1
    Trophy Points:
    33
    #2
    I got it,
    if(empty($this->_errors)) {$this->_passed = true; } must be in outer and not inner loop.
     
    iago111, Aug 30, 2020 IP
    Efetobor Agbontaen likes this.