[eluser]vickatg[/eluser]
I do know this topic has been discussed alot, i went through almost all discussions regarding this topic yet i couldn’t solve my problem.
My case is that, i want to validate if the username a user wants to register already exists in the database, if it exists registration form should prompt back demanding a new username.
I did all i could to implement the scenario unfortunately my call_back function isn’t working.
This is what i did.
1. created
MY_Form_validation file that extends the validation class in application/libraries
Code:
if (!defined('BASEPATH')) exit('No direct script access allowed');
class MY_Form_validation extends CI_Form_validation
{
function run($module = '', $group = '') {
(is_object($module)) AND $this->CI =& $module;
return parent::run($group);
}
}
2.created a controller as follows
Code:
class Syst_conf_controller extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('syst_conf/syst_conf_model');
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<div class="validation_error">', '</div>');
}
public function user()
{
if($this->input->post('new_user'))
{
// field name, error message, validation rules
$this->form_validation->set_rules('first_name', 'First Name', 'trim|required|xss_clean');
$this->form_validation->set_rules('surname','Surname','trim|required|xss_clean');
$this->form_validation->set_rules('phone_number','Phone Number','trim|required|numeric|xss_clean');
$this->form_validation->set_rules('email_address','Email Address','trim|required|valid_email');
$this->form_validation->set_rules('rank','Rank','trim|required|xss_clean');
$this->form_validation->set_rules('username','Username','callback__username_check');
$this->form_validation->set_rules('password','Password','trim|required|matches[cpassword]');
$this->form_validation->set_rules('cpassword','Password Confirmation','trim|required');
if ($this->form_validation->run($this) == FALSE)
{
$content['contents'] = NULL;
$content['form_data'] = $this->syst_conf_model->load_user_groups();
$this->load->view('syst_conf/new_user',$content);
}
else
{
$query = $this->syst_conf_model->create_user();
if($query)
{
$content = array('contents' => 'user successfully added');
$this->load->view('syst_conf/new_user',$content);
}
else{
$content = array('contents' => 'an error occured while updating data');
$this->load->view('syst_conf/new_user',$content);
}
}
}
else
{
$content['contents'] = NULL;
$content['form_data'] = $this->syst_conf_model->load_user_groups();
$this->load->view('syst_conf/new_user',$content);
}
}
//this functions validates if the selected username exists in the database
function _username_check()
{
$username= $this->input->post('username');
$result = $this->syst_conf_model->check_username($username);
if(strcasecmp($result, "TRUE")==0)
{
$this->form_validation->set_message('username_check', 'please choose another username, that one is already taken');
return FALSE;
}
else
{
return TRUE;
}
}
}
3. below is my model
Code:
class Syst_conf_model extends CI_Model {
function __construct() {
parent::__construct();
}
function check_username($username)
{
$sql = "select username from tbl_user where username='$username'";
$query = $this->db->query($sql);
if($query->num_rows() == 0)
{
return FALSE;
}
else
{
return TRUE;
}
}
The question is, what should i do to trigger my call_back function?