Reputation: 664
I am trying to extend form validation library with this code:
class MY_Form_validation extends CI_Form_validation {
public function __construct() {
parent::__construct();
}
public function check_captcha($captcha)
{
if ($captcha == $this->session->userdata('captcha'))
{
return TRUE;
} else
{
$this->form_validation->set_message('check_captcha', "Please copy captcha again.");
return FALSE;
}
}
}
When I run validation I get this error:
Message: Undefined property: MY_Form_validation::$session
Filename: libraries/MY_Form_validation.php
I tried to load the session library at construct but I got again error. Is there any option to use session library inside or should I pass the value from session as 2nd parameter?
Upvotes: 0
Views: 2493
Reputation: 507
libraries in codeigniter must get an instance of the CI object (what you'd think of as $this when calling session).
add the following to your contructor function:
$CI =& get_instance();
now when referencing properties/methods of the CI object, just use $CI instead of $this:
if($captcha == $CI->session->userdata('captcha'))
and so on. So use $CI when needing something outside of the library, otherwise, you can use $this
Upvotes: 0
Reputation: 3149
If you need to access another loaded library from within a library, you need access to the codeigniter instance.
As you are extending the existing Form Validation library, you can access the codeigniter instance by using:
$this->CI
So, in your case, you can access the session by using:
$this->CI->session->userdata('captcha')
Upvotes: 2