Welcome Guest, Not a member yet? Register   Sign In
undefined property errors
#1

[eluser]iamdesign[/eluser]
Hi there,

I just started of with CI and I have to say => wonderful.

Although I still need some help with the basics Smile

At my labwebsite I'm trying to create a loginscript.
But I keep running into undefined property errors.

This is my Controller (loaded standard):
Code:
class Login extends Controller{

    function Login()
    {
        parent::Controller();
        
        $this->load->helper('url');
        $this->load->helper('form');
        $this->load->library('validation');
    }

    function index()
    {
        $this->load->view('login_view');
    }
    
    function checkLogin()
    {            
        $rules['username'] = "trim|required|min_length[5]|max_length[12]|xss_clean";
        $rules['password'] = "trim|required|matches[passconf]|md5";
        
        $this->validation->set_error_delimiters('<div class="error">', '</div>');
        $this->validation->set_rules($rules);
        
        $fields['username'] = 'Username';
        $fields['password'] = 'Password';
        
        $this->validation->set_fields($fields);
        
        
        if ($this->validation->run() == FALSE)
        {
            $this->load->view('login_view');
        }
        else
        {
            echo "ok";
        }
    }
    
}

And this is my HTML form

Code:
<h2>Login</h2>
                &lt;?= form_open('login/checkLogin') ?&gt;
                    <label for="txtUsername">Username</label>
                    &lt;input type="text" name="username" id="txtUsername" value="&lt;?= $this-&gt;validation->username;?&gt;" /><br />
                    <label for="txtPassword">Password</label>
                    &lt;input type="password" name="password" id="txtPassword" value="&lt;?= $this-&gt;validation->password;?&gt;" /><br />
                    
                     <button type="submit" name="btnLogin" class="btnLogin" value=""><img src="&lt;?= base_url(); ?&gt;images/layout/login/submit.png" alt="Login" /></button>
                &lt;/form&gt;
                
                &lt;?= $this->validation->error_string; ?&gt;

I know that the error is there because when he loads the controller he loads the default view and there he doesn't know the validation class.
But offcourse how can I overcome this error?

Should I load the validation class globally? of is there a cleaner way Smile

I hope somebody can help me overcome my first CI troubles ;-)

Thanks alot,
Frederik
#2

[eluser]iamdesign[/eluser]
ok i solved the problem by adding this to the index function

Code:
function index()
    {
        $fields['username'] = 'username';
        $fields['password'] = 'password';
        
        $this->validation->set_fields($fields);
        
        $this->load->view('login_view');
    }

allthough i don't know if this is the right way of handling this problem. can somebody get me on track Smile

thnx
#3

[eluser]Pascal Kriete[/eluser]
You can just load checkLogin from the get-go (or move all your code to index Smile ). It 's almost as if it gets skipped the first time, since there is no post data. It'll just load your login view.
#4

[eluser]wiredesignz[/eluser]
This may help:

http://ellislab.com/forums/viewthread/69258/

and maybe this:

http://ellislab.com/forums/viewthread/69253/
#5

[eluser]iamdesign[/eluser]
[quote author="wiredesignz" date="1201673886"]This may help:

http://ellislab.com/forums/viewthread/69258/

and maybe this:

http://ellislab.com/forums/viewthread/69253/[/quote]

thnx it helped me bigtime :-)

In the mean while I had allready a working version online, but I gonna adapt my code a bit, because I saw some better ways of doing it in your links Smile

great great!




Theme © iAndrew 2016 - Forum software by © MyBB