0

hi have been doing a bit more of the tutorial and im trying to show errors if someone doesnt fill in information but the required notice isnt showing all im getting is
is required.
is required.
is required.
is required.
when it should show the following:
username is required etc

can someone check the code for me
Validate.php

<?php
public function __construct(){
        $this->_db = DB::getInstance();
    }
    public function check($source, $items = array()){
        foreach($items as $item => $rules){
            foreach($rules as $rule => $rule_value){
                $value = trim($source[$item]);
                $item = escape($item);

                if($rule === 'required' && empty($value)){
                    $this->addError("{$item} is required.");
                } else if(!empty($value)){
                    switch($rule){
                        case 'min':
                            if(strlen($value) < $rule_value){
                                $this->addError("{$item} must be a minimum of {$rule_value} characters.");
                            }
                            break;
                        case 'max':
                            if(strlen($value) > $rule_value){
                                $this->addError("{$item} should not exceed {$rule_value} characters.");
                            }
                            break;
                        case 'matches':
                            if($value != $source[$rule_value]){
                                $this->addError("{$rule_value} must match {$item}");
                            }
                            break;
                        case 'unique':
                            $check = $this->_db->get($rule_value, array($item, '=', $value));
                            if($check->count()){
                                $this->addError("{$item} already exists.");
                            }
                            break;
                    }    
                } 
            }    
        }
        if(empty($this->_errors)){
            $this->_passed = true;
        }
        return $this;
    }

    private function addError($error){
        $this->_errors[] = $error;
    } 

    public function errors(){
        return $this->_errors;
    } 

    public function passed(){
        return $this->_passed;
    }      
}

Register.php

<?php
require_once 'core/init.php';

if(Input::exists()) {
   $validate = new Validate();
   $validation = $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 { 
   foreach ($validate->errors() as $error) {
    echo $error,'<br>'; 
    } 
    }
    }
?>

register form

<form action="" method="post"> <table> <tr> <td><label for="username">Username</label></td> </tr> <tr> <td><input type="text" name="username" id="username"  value="<?php echo escape(Input::get('username')); ?>" autocomplete="off"/></td> </tr> <tr> <td><label for="password">Password</label></td> </tr> <tr> <td><input type="password" name="password" id="password"/></td> </tr> <tr> <td><label for="password_again">Repeat Password</label></td> </tr> <tr> <td><input type="password" name="password_again" id="password_again"/></td> </tr> <tr> <td><label for="name">Nickname</label></td> </tr> <tr> <td><input type="text" name="name" id="name" value="<?php echo escape(Input::get('name')); ?>"/></td> </tr> <tr> <td><input type="hidden" name="token" value=""/><input type="submit" value="Register" /></td> </tr> </table> </form>

ive been through tutorial twice and still cannot get it working and help much appreciated x

3
Contributors
4
Replies
19
Views
1 Year
Discussion Span
Last Post by janicemurby
0

Please format the register form with proper intentation. Also, you don't indicate where your bogus output is being generated. I can't find it anywhere in your example code (either PHP or HTML).

0
 if($rule === 'required' && empty($value)){
                    $this->addError("{$item} is required.");

is giving you an empty $item?

0

disregard this one ive found out the owner of the tutorial as a updated script so ill put this as completed

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.