CodeIgniter Forums

Full Version: oh what ... oh what I'm I doing wrong in this validation piece?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]skattabrain[/eluser]
Pretty straight forward ... she won't validate the custom callback_username_check.

Here's some controller code and the callback listed below it.

Code:
function create()
    {
        $d = array();
        $this->load->model('Cart');        
        $d['cart'] = $this->Cart->get_cart();
        $this->load->helper('form');
        $this->load->library('validation');
        
        $rules['first'] = "trim|required|min_length[2]|xss_clean";
        $rules['last'] = "trim|required|min_length[2]|xss_clean";
        $rules['telephone'] = "trim|required|min_length[10]|xss_clean";
        $rules['username'] = "trim|required|min_length[3]|xss_clean|callback_username_check";
        $rules['password'] = "trim|required|min_length[5]|max_length[12]|xss_clean";
        $rules['password2'] = "trim|required|matches[password]|xss_clean";
        $rules['email'] = "trim|required|valid_email|xss_clean";
        $this->validation->set_rules($rules);
        
        $fields['first'] = 'First Name';
        $fields['last'] = 'Last Name';
        $fields['telephone'] = 'Telephone';
        $fields['email'] = 'Email';
        $fields['username'] = 'Username';
        $fields['password'] = 'Password';
        $fields['password2'] = 'Confirm Password';
        $this->validation->set_fields($fields);
        
        
        
        $this->validation->set_error_delimiters('<div class="error">', '</div>');
        
        if ($this->validation->run() == FALSE)
        {
            $form = form_open('mycart/create');
            $form .= form_fieldset('Create an account');
            $form .= $this->validation->error_string;
            $form .= form_label('First Name', 'first');
            $form .= form_input('first', $this->validation->first);
            $form .= form_label('Last Name', 'last');
            $form .= form_input('last', $this->validation->last);
            $form .= form_label('Telephone', 'telephone');
            $form .= form_input('telephone', $this->validation->telephone);
            $form .= form_label('Email', 'email');
            $form .= form_input('email', $this->validation->email);
            $form .= form_label('Username', 'username');
            $form .= form_input('username', $this->validation->username);
            $form .= form_label('Password', 'password');
            $form .= form_password('password', $this->validation->password);
            $form .= form_label('Confirm Password', 'password2');
            $form .= form_password('password2', $this->validation->password2);
            $form .= form_submit('submit', 'submit');
            $form .= form_fieldset_close();
            $form .= form_close();
            
            $d['form'] = $form;
            $this->load->view('cart_page', $d);    
        }
        else
        {
            $customer_id = $this->Cart->insert_new_user($this->validation->first, $this->validation->last, $this->validation->telephone, $this->validation->email, $this->validation->username, $this->validation->password);
            $this->session->set_userdata('customer_id', $customer_id);
            redirect('mycart/checkout2');
        }
    }



    function username_check($username)
    {
        $query = $this->db->get_where('customer', 'username', $username);    
        if ($query->num_rows() > 0) {
            $this->validation->set_message('username_check', 'The username '.$username.' is already in use by another customer');
            return FALSE;
        } else {
            return TRUE;
        }
    }

El Forum

[eluser]xwero[/eluser]
Code:
$query = $this->db->get_where('customer', array('username'=>$username));

El Forum

[eluser]skattabrain[/eluser]
duh ... thanks xwero. i'd like to say it worked for me at one point when i had it in a model ... but i chased my tail a bit on that one, so i probably didn't. thanks again.