CodeIgniter Forums
validation error - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: validation error (/showthread.php?tid=78887)



validation error - fdev - 03-22-2021

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;



RE: validation error - InsiteFX - 03-23-2021

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

PHP Code:
$this->db db_connect(); // REMOVE THIS 



RE: validation error - paulbalandan - 03-26-2021

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_');