The Code
function create_member(){
$this->load->library('form_validation');
$this->form_validation->set_message('check_if_username_exists', "This Username is sadly already taken. Nice try though!.");
$this->form_validation->set_message('check_if_email_exists', "Someone has already signed up with this email address. Our sincerest apologies!.");
$this->form_validation->set_rules('email', 'Email Address', 'trim|required|valid_email|callback_check_if_email_exists');
$this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[4]|callback_check_if_username_exists');
$this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]');
$this->form_validation->set_rules('password_confirm', 'Password Confirmation', 'trim|required|matches[password]');
if($this->form_validation->run() == FALSE)//didn't validate {
$this->signup();}
else{
$this->load->model('membership_model');
if($query = $this->membership_model->create_member()){
$this->data['account_created'] = 'Your account has been created.
You may now login';
$this->index();}
else{ $this->signup(); } } }
function check_if_username_exists($requested_username) { //custom callback function
$this->load->model('membership_model');
$username_available = $this->membership_model->check_if_username_exists($requested_username);
if($username_available){ return TRUE;}
else { return FALSE; } }
This controller is getting the form input from the "Sign-Up" form. It loads the form validation library that is build into CodeIgniter. It then sets validation error messages that the user will see if the input they select is not validated. If the input is validated, the data goes to the membership model, where the new user is stored in the MySql database.
The second function checks if the username that the user has selected is available. It does this by checking whether the username that was inputed already exists in the database table "users". If it does, the the function returns false and causes a validation error informing the user that the username is already taken.