Welcome Guest, Not a member yet? Register   Sign In
validation error
#1

Code:
I am having an error "Call to a member function setRules() on null"

Call to a member function setRules() on null
Code:
SYSTEMPATH/BaseModel.php at line 1421
Code:
return $this->validation->setRules($rules, $this->validationMessages)->run($data, null, $this->DBGroup);


BaseModel.php
<?php
namespace App\Models;
use CodeIgniter\Model;


class BaseModel extends Model
{
    public $db;
    public function __construct()
    {
        $this->db = db_connect();
        $this->db->setPrefix('fdev_');
    }
}

<?php
namespace App\Models;

CustomersModel.php
class CustomersModel extends BaseModel
{
    protected $table = 'customers';
    protected $primaryKey = 'id';
    protected $useAutoIncrement = true;
    protected $allowedFields = [
        'company_name',
    ];
    protected $beforeInsert = ['beforeInsert'];
    protected $afterInsert = ['afterInsert'];
    protected $beforeUpdate = ['beforeUpdate'];
    protected $afterUpdate = ['afterUpdate'];
    protected $useTimestamps = true;
    protected $createdField  = 'created_at';
    protected $updatedField  = 'updated_at';
    protected $deletedField  = 'deleted_at';
    protected $validationRules    = [
        'company_name' => 'required',
    ];
    protected $validationMessages = [];
    protected $skipValidation     = false;
Reply
#2

The main Model already has $this->db but your redefining it in you BaseModel __construct.

PHP Code:
$this->db db_connect(); // REMOVE THIS 
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

change your constructor to this
PHP Code:
public function __construct()
{
    
$db db_connect();
    
$validation = \Config\Services::validation(nullfalse);
    
parent::__construct($db$validation);

    
$this->db->setPrefix('fdev_');

Reply




Theme © iAndrew 2016 - Forum software by © MyBB