Welcome Guest, Not a member yet? Register   Sign In
callback not functioning
#1

[eluser]CIfan1000[/eluser]
Hi,
I am new to CI and so please excuse me if I have made simple mistakes.
The function username_check is not being called as part of the validation of the username field.
Any help would be very much appreciated.
Thanks!

Code:
<?php

class Login extends Controller {

    function index()
    {
        $this->load->library('session');

        // Load CI helpers required for form validation:
        $this->load->helper(array('form', 'url'));

        // Load the validation library:
        $this->load->library('validation');
        
        // Define the validation rules for each field:
        // The names must be the field names in the form:
        $rules['LoginUsername']         = "trim|required|min_length[5]|max_length[12]|callback_username_check | xss_clean|alpha_dash";
        $rules['LoginUserPassword']     = "trim|required|min_length[5]|max_length[12]|xss_clean|alpha_dash | md5";

        $this->validation->set_rules($rules);
        
        // Repopulate the form field with the submitted data
        // The array keys are the actual names of the form fields,
        // the value represents the full name that you want shown in the error message.
        $fields['LoginUsername'] = 'Username';
        $fields['LoginUserPassword'] = 'Password';

        $this->validation->set_fields($fields);
        
        

        if ($this->validation->run() == FALSE)
        {
            // If validation is false then re-display the form:

            $data['title'] = "My site";
            $data['bodyview'] = "login/login";
            $this->load->view('templates/template01',$data);
        }
        else
        {
            //If validation is true/ok, check if the username is in the database:
            
            // Load the CI database thingy:
            $this->load->database();
            
            //Get the username into a variable:
            $UserName = $_POST['LoginUsername'];
            $UserPassword = $_POST['LoginUserPassword'];

            // Check the user table if the username exists:
            $query = $this->db->query("SELECT * FROM users WHERE UserName='{$UserName}' AND UserPassword = '{$UserPassword}'");
            
            // If the user is already in the table then num-rows will be > 0:
            if ($query->num_rows() > 0)
                {
                    //echo "Username and password authenticated.";

                    // Store the username as a session variable
                    $this->session->set_userdata('Username', $UserName);
                    
                    // Store the user's authentication status as a session variable
                    $this->session->set_userdata('Authenticated', 'True');
                    
                    // Echo the username session varialbes as a test:
                    
                    $session_username = $this->session->userdata('Username');
                    echo $session_username;
                    $session_userauthenticated = $this->session->userdata('Authenticated');
                    echo $session_userauthenticated;
                    
                    // Load the template with correct view:
                    $data['title'] = "Mike's site";
                    $data['bodyview'] = "login/loginsuccess";
                    $this->load->view('templates/template01',$data);

                }
            else
                {
                    // If username and password are not in db:
                    echo "Username and password do not match.";
                    //$this->validation->set_message('required', 'Username and password do not match.');


                    // Re-display the login form:
                    // Load the template with correct view:
                    $data['title'] = "My site";
                    $data['bodyview'] = "login/login";
                    $this->load->view('templates/template01',$data);

                }
           }
          
       }
      
    function username_check($str)
    {
        if ($str == 'test1')
        {
            $this->validation->set_message('username_check', 'The %s field can not be the word "test1"');
            return FALSE;
        }
        else
        {
            return TRUE;
        }
    }
}
?>
#2

[eluser]CIfan1000[/eluser]
I experimented and found the solution:

There can be no spaces between validation parameters and the separator | - I did have spaces, when I removed them, the call back worked.

$rules['LoginUsername'] = "trim|required|min_length[5]|max_length[12]|callback_username_check | xss_clean|alpha_dash";

callback_username_check |
Had to be changed to:
callback_username_check|
#3

[eluser]CIfan1000[/eluser]
PS I love working with CI!




Theme © iAndrew 2016 - Forum software by © MyBB