[eluser]behnampmdg3[/eluser]
Hello;
In the example below I want to keep my controller small so I keep all my validations in another place. Well I think a good way would be to keep them in libraries/my_functions. But for some reason I cannot call any functions
from libraries/my_functions.
In the example I can easily call
validate_search() from my controller but I cannot call any functions from there (from my_functions)!
For example the callback function
callback_valid_product_class below doesnt work, or test() does not work either.
2 questions:
1 - Why cant I call these functions?
2 - Is this the proper way of keeping controller light?
Thank you
Controller:
Code:
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Products extends CI_Controller {
public function search()
{
if(!$this->input->post('submit'))
{
redirect(base_url('products'), 'location', 301);
}
$this->load->library('my_functions');
if($this->my_functions->validate_search()) //Works fine
{
echo 'valid';
}
else
{
$this->show_form();
$this->right_column_and_footer();
}
}
}
my_functions.php
Code:
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class My_functions {
public function validate_search()
{
$CI =& get_instance();
$CI->form_validation->set_rules('class', 'Product Class', 'numeric|xss_clean|callback_valid_product_class');
if ($CI->form_validation->run() == FALSE)
{
return FALSE;
}
else
{
return TRUE;
}
}
public function valid_product_class($class)
{
exit();
$CI->load->model('model_products');
if($CI->model_products->check_class($class))
{
return true;
}
else
{
$CI->form_validation->set_message('class', 'Invalid class');
return FALSE;
}
}
$CI->test();
public function test()
{
echo "Hi";
}
}