Welcome Guest, Not a member yet? Register   Sign In
library class in my application
#1

[eluser]andygo[/eluser]
I am trying to create a library class in my application.
I am experimenting with the form validation class example from the docs.

I seperated soem of the code and put it in a library file.
It works except for the call back function. I havent changed anything so I was wondering if there's something I'm missing in having it in a library class.....

Here's the library class code:

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/*
*
*
*/

class Form_data_lib
{

public function __construct()
{
$this->ci =& get_instance();
$this->ci->load->library('form_validation');
}//end construct

//janitor
public function janitor()
{
$this->ci->form_validation->set_rules('username', 'Username', 'callback_username_check');
$this->ci->form_validation->set_rules('password', 'Password', 'required');
$this->ci->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
$this->ci->form_validation->set_rules('email', 'Email', 'required');

  if ($this->ci->form_validation->run() == FALSE)
  {
  return '0';
  }
  else
  {
  return '1';
  }
}

public function username_check($str)
{

  if ($str == 'test')
  {
  $this->ci->form_validation->set_message('username_check', 'The %s field can not be the word "test"');
  return FALSE;
  }
  else
  {
  return TRUE;
  }
}


}//end class

/* End of file Form_data_lib.php */
/* Location: ./application/libraries/Form_Data_lib.php */

Any help appreciated.
BTW: It works fine when set up as per the docs.
#2

[eluser]InsiteFX[/eluser]
Place your callbacks in a MY_Controller
#3

[eluser]andygo[/eluser]
Thanks InsiteFX, much appreciated

I created a MY_Controller in application/core and extended it in the controller.
I referenced my library file and now my library has access to the callback function in the MY_Controller..


In case this helps anyone here is my code...
Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Controller extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->library('form_data_lib');
}

public function username_check($str)
{
  if ($str == 'test')
  {
   $this->form_validation->set_message('username_check', 'The %s field can not be the word "test"');
   return FALSE;
  }
  else
  {
   return TRUE;
  }
}


}
// END Class

/* End of file .php */
/* Location: .php */




Theme © iAndrew 2016 - Forum software by © MyBB