Welcome Guest, Not a member yet? Register   Sign In
Do i HAVE to use a helper file?
#1

[eluser]ryeguy[/eluser]
I want to call validate_username but php says the function is undefined. Do I have to use a helper file to use internal functions or something?

Please ignore the actual code this is my first time using CI I am just figuring out how everything works.

Code:
<?php

class User extends Controller
{
    function add($username, $password)
    {
        $this->load->library('form_validation');
        
        
        $data['variables'] = array("username" => $username,
                                   "password" => hash("sha256", $password),
                                   "user_valid" => validate_username($username));

        $this->load->view('output', $data);
    }
    
    function validate_username($username)
    {
        return
        $this->form_validation->min_length($username,4) &&
        $this->form_validation->max_length($username,15) &&
        $this->form_validation->alpha_numeric($username) &&
        is_string($username[0]);// && //make sure the first character is not a number
    }
}

/* End of file user.php */
/* Location: ./system/application/controllers/user.php */
#2

[eluser]Michael Wales[/eluser]
You are in an object oriented development environment - you need to tell PHP what class to find that particular function in. In this case, $this works just fine.

Code:
$data['variables'] = array("username"   => $username,
                           "password"   => hash("sha256", $password),
                           "user_valid" => $this->validate_username($username));

On a side note, you probably want to make that function private by prepending an underscore (function _validate_username). Otherwise it is accessible via the URI.




Theme © iAndrew 2016 - Forum software by © MyBB