Your Location is: Home > Php

Fatal error: Uncaught Error: Call to a member on validatin condition

From: Dominica View: 4761 Farzad Ghasempour 
 php

Question

I searched in site, but I didn't find anything, unfortunately. I put my problem below: its my DB:

class DB
{
    private static $_instance = null;
    private $_pdo,
            $_query,
            $_error = false,
            $_result,
            $_count;

    private function __construct()
    {
        try {

            $this->_pdo = new PDO('mysql:host=' . Config::get('mysql/host') . ' ;dbname=' . Config::get('mysql/db'), Config::get('mysql/username'), Config::get('mysql/password'));

        } catch (PDOException $e) {
            die($e->getMessage());
        }
    }

    public static function getInstance()
    {
        if (!isset(self::$_instance)) {
            self::$_instance = new DB();
        }
        return self::$_instance;
    }

    public function query($sql, $params = [])
    {
        $this->_error = false;
        if ($this->_query = $this->_pdo->prepare($sql)) {
            $x = 1;
            if (count($params)) {
                foreach ($params as $param) {
                    $this->_query->bindValue($x, $param);
                    $x++;
                }
            }

            if ($this->_query->execute()) {
                $this->_result = $this->_query->fetchAll(PDO::FETCH_OBJ);
                $this->_count = $this->_query->rowCount();
            } else {
                $this->_error = true;
            }
        }
        return $this;
    }

    public function count()
    {
        return $this->_count;
    }
    public function result()
    {
        return $this->_result;
    }
}

and my Validation is:

class Validate
{
    private $_passed = false,
        $_errors = [],
        $_db = null;

    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 minimum of {$rule_value} characters.");
                            }
                            break;

                        case 'max':
                            if (strlen($value) > $rule_value) {
                                $this->addError("{$item} must be maximum of {$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, [$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;
    }

}

and my register page:

<?php
require_once "core/init.php";
if (Input::exists()) {
    if (Token::check(Input::get('token'))) {

        $validate = new Validate();
        $validation = $validate->check($_POST, array(

            'name' => [
                'required' => true,
                'min' => 3,
                'max' => 50
            ],
            'username' => array(
                'required' => true,
                'min' => 2,
                'max' => 32,
                'unique' => 'users'
            ),
            'password' => array(
                'required' => true,
                'min' => 6,
                'max' => 64
            ),
            'password_again' => array(
                'required' => true,
                'matches' => 'password'
            ),
            'email' => array(
                'required' => true,
                'unique' => 'users'
            ),
            'selectSex' => array(
                'required' => true
            )

        ));

        if ($validation->passed()) {
            echo "OK";
        } else {
           print_r($validation->errors());
        }
    }
}

?>
<?php require_once "Login/INC/header.php"; ?>
<body class="hold-transition register-page">
<div class="register-box">
    <div class="register-logo">
        <b>ثبت نام در سایت</b>
    </div>

    <div class="card">
        <div class="card-body register-card-body">
            <p class="login-box-msg"></p>

            <form action="" method="post">
                <div class="input-group mb-3">
                    <input type="text" name="name" class="form-control" placeholder="نام و نام خانوادگی">
                    <div class="input-group-append">
                        <span class="fa fa-user input-group-text"></span>
                    </div>
                </div>
                <div class="input-group mb-3">
                    <input type="text" name="username" class="form-control" placeholder="نام کاربری">
                    <div class="input-group-append">
                        <span class="fa fa-user input-group-text"></span>
                    </div>
                </div>

                <div class="input-group mb-3">
                    <input type="password" name="password" class="form-control" placeholder="رمز عبور">
                    <div class="input-group-append">
                        <span class="fa fa-lock input-group-text"></span>
                    </div>
                </div>
                <div class="input-group mb-3">
                    <input type="password" name="password_again" class="form-control" placeholder="تکرار رمز عبور">
                    <div class="input-group-append">
                        <span class="fa fa-lock input-group-text"></span>
                    </div>
                </div>
                <div class="input-group mb-3">
                    <input type="email" name="email" class="form-control" placeholder="ایمیل">
                    <div class="input-group-append">
                        <span class="fa fa-envelope input-group-text"></span>
                    </div>
                </div>

                <div class="form-group">
                    <select class="form-control" name="selectSex">
                        <option value="">جنسیت خود را انتخاب نمایید</option>
                        <option value="مرد">مرد</option>
                        <option value="زن">زن</option>
                    </select>
                </div>


                <div class="row">

                    <!-- /.col -->
                    <div class="col-md">
                        <input type="hidden" name="token" value="<?php echo Token::generate(); ?>">
                        <button type="submit" name="btnreg" class="btn btn-primary btn-block btn-flat">ثبت نام</button>
                    </div>
                    <!-- /.col -->
                </div>
            </form>

            <div class="social-auth-links text-center">
                <p>- یا -</p>
                <a href="#" class="btn btn-block btn-primary">
                    <i class="fa fa-facebook mr-2"></i>
                    ثبت نام با اکانت فیس بود
                </a>
                <a href="#" class="btn btn-block btn-danger">
                    <i class="fa fa-google-plus mr-2"></i>
                    ثبت نام با گوگل
                </a>
            </div>

            <a href="login.html" class="text-center">من قبلا ثبت نام کرده ام</a>
        </div>
        <!-- /.form-box -->
    </div><!-- /.card -->
</div>
<!-- /.register-box -->
<?php require_once "Login/INC/footer.php"; ?>

After I click on register, I faced to the this error:

Fatal error: Uncaught Error: Call to a member function count() on bool in D:\xampp\htdocs\portal\classes\Validate.php:50 Stack trace: #0 D:\xampp\htdocs\portal\register.php(10): Validate->check(Array, Array) #1 {main} thrown in D:\xampp\htdocs\portal\classes\Validate.php on line 50

But, every thing work properly when I delete this code:

case 'unique':
$check = $this->_db->get($rule_value, [$item, '=', $value]);
if ($check->count()) {
$this->addError("{$item} already exists.");
}
break;

Before, I wrote this code and It was working properly, But, now It faced to error (count()) and I don't the reason..... Why? Let me know it's reason

Best answer