Welcome Guest, Not a member yet? Register   Sign In
using validation_errors() with database
#1

[eluser]jordanarseno[/eluser]
Hey Guys,

I have a sign-up form at localhost/CI/login/signup.
The form validation checks for valid username, password, email - all that junk properly.

I'm trying to take the username that is given, say johnsmith, verify it does not exist and add it to the database. If the username does exist, I want the validation_errors() box to display "username johnsmith exists - try again"

The following is a function called create_user() - part of my Signup Controller which is launched when users click the Create User button in the view:

Code:
function create_user()
    {
        $this->form_validation->set_rules("firstname", "Name", "trim|required|max_length[20]");
        $this->form_validation->set_rules("lastname", "Last Name", "trim|required|max_length[20]");
        $this->form_validation->set_rules("email", "Email", "trim|required|valid_email|max_length[40]");
        $this->form_validation->set_rules("username", "Username", "trim|required|min_length[4]|max_length[32]");
        $this->form_validation->set_rules("password", "Password", "trim|required|min_length[4]|max_length[32]");
        $this->form_validation->set_rules("password2", "Password Confirm", "trim|required|matches[password]");
        
        //run form validation, if fail refresh page ans show errors
        if($this->form_validation->run() == FALSE ) $this->index();
        //otherwise...
        else
        {
            //load the users model and check if the username exists
            $this->load->model("users_model");
            $row = $this->users_model->username_exists();
            //if it does not exist
            if(!$row)
            {
                //create the user
                $this->users_model->create_user();
                $data["main_content"] = "signup_successful_view";
                $this->load->view("includes/logged_out_template", $data);
                
            }
            else
            {
                $this->form_validation->set_message('matches',"Username Exists");
                $this->form_validation->matches($row->username, 'username');
                $this->form_validation->run();
                $this->index();
            }
        }
    
    }

The username_exists() function inside the users_model is designed to return false if it DOES NOT exist, and to return the username itself (in the form of a row) if it DOES exist:

Code:
function username_exists()
    {
        $this->db->select("username");
        $this->db->from("users");
        $this->db->where("username", $this->input->post("username"));
        
        $q = $this->db->get();
        if($q->num_rows()==1)  return $q->row();
        else return FALSE;
    }

The username_exists() function is correct, what I'm really having trouble with is the last else chunk in the large controller code above.

Any ideas? - Thanks a million!


Messages In This Thread
using validation_errors() with database - by El Forum - 10-16-2010, 06:10 AM
using validation_errors() with database - by El Forum - 10-16-2010, 06:52 AM
using validation_errors() with database - by El Forum - 10-16-2010, 07:11 AM
using validation_errors() with database - by El Forum - 10-17-2010, 09:46 AM
using validation_errors() with database - by El Forum - 11-04-2010, 09:00 PM



Theme © iAndrew 2016 - Forum software by © MyBB