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!
#2

[eluser]Bas Vermeulen[/eluser]
Hi, you should use callbacks for this: click

This will make your life easier Wink
#3

[eluser]jordanarseno[/eluser]
omg, wonderful!
not to mention the example listed is EXACTLY what I need to do.

THANKS BAS!
#4

[eluser]Bas Vermeulen[/eluser]
Yeah Wink you're welcome!
#5

[eluser]Fandy[/eluser]
dh,

i use this code to check username exist in database.
but, when i run my code.It only,. check one record in database.

Please help me..

This my controller

Code:
....
function username_check($str){
    $data['user'] = $this->formModel->getData();
            
    foreach($data['user'] as $usr){
        if($str == $usr->username){
        $this->validation->set_message('username_check','Username already exist');
            }else{
            return TRUE;
            }
        }
....

This my model
Code:
<?php
    class FormModel extends Model{
    
        function __construct(){
            parent::Model();
        }
        
        function getData(){
            return $this->db->query('SELECT username FROM user');
        }
    }

?>




Theme © iAndrew 2016 - Forum software by © MyBB