Form validation in Codeigniter 3 using different database

I got in trouble with Codeigniter 3 using the form validation library; I have to check that the email address submitted by a user into aregistration form is unique into the users database. I use two databases in my project, the users one is not the default. To perform the email check I use the following code:

$this->form_validation->set_rules('email','Email','trim|required|valid_email|is_unique[users.email]');

I got an error about the missing users table into the default database, so I've realized that CI 3 looks the email to check into a default database ... not the correct users database, even if in the construct I load the correct model/database.

Is there a way to perform the check into a different database using the form validation above?

Thanks for any feedback

UPDATE Below the code I use to load the model in the controller

function __construct()
{
    parent::__construct();
    $this->load->model("admin/user_model","user");
}

Below the code of the User_model

// Database
private $auth_db;

// Tables
private $table_users = 'users';

public function __construct()
{
    parent::__construct();
    $this->auth_db = $this->load->database('auth', true);
}

and...finally...in the config file the database configuration

$active_group = 'default';
$query_builder = TRUE;

$db['default'] = array(
    'dsn'   => '',
    'hostname' => 'localhost',
    'username' => 'xxxxxxxxx',
    'password' => 'xxxxxxxxx',
    'database' => 'vfr_main',
    'dbdriver' => 'mysqli',
    'dbprefix' => '',
    'pconnect' => FALSE,
    'db_debug' => TRUE,
    'cache_on' => FALSE,
    'cachedir' => '',
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_unicode_ci',
    'swap_pre' => '',
    'encrypt' => FALSE,
    'compress' => FALSE,
    'stricton' => FALSE,
    'failover' => array(),
    'save_queries' => TRUE
);

$db['auth'] = array(
    'dsn'   => '',
    'hostname' => 'localhost',
    'username' => 'xxxxxxxxxxx',
    'password' => 'xxxxxxxxxxx',
    'database' => 'vfr_auth',
    'dbdriver' => 'mysqli',
    'dbprefix' => '',
    'pconnect' => FALSE,
    'db_debug' => TRUE,
    'cache_on' => FALSE,
    'cachedir' => '',
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_unicode_ci',
    'swap_pre' => '',
    'encrypt' => FALSE,
    'compress' => FALSE,
    'stricton' => FALSE,
    'failover' => array(),
    'save_queries' => TRUE
);


Read more here: https://stackoverflow.com/questions/68436721/form-validation-in-codeigniter-3-using-different-database

Content Attribution

This content was originally published by Federico at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: