Welcome Guest, Not a member yet? Register   Sign In
Functions can't be called from libraries.
#1

[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";
                 }
}
#2

[eluser]jairoh_[/eluser]
1. have you called the library?
2. try putting $CI =& get_instance(); into a constructor
3. are there errors?
#3

[eluser]TheFuzzy0ne[/eluser]
By default, CodeIgniter checks in 2 places for callbacks -- Your controller, and the validation class itself. If you have your callbacks anywhere else, you will either need to modify the form validation class, or you will need to add an alias method to either the form validation class or your controller. I think the best option is to have a separate version of the form validation class for each "section" of your Web site. For example, one for user validation, one for product validation and so on. Then you can simply load the class you need, and all of the relevant methods are there.

./application/libraries/Product_validation.php
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

include BASEPATH . 'libraries/Form_validation' . EXT;

class Product_validation extends CI_Form_validation {
    protected $CI;

    function __construct()
    {
        parent::__construct();
        
        $this->CI =& get_instance();
    }

    public function valid_product_class($class)
    {
        $this->CI->load->model('model_products');
        
        if( ! $this->CI->model_products->check_class($class))
        {
            $this->set_message('valid_product_class', 'Invalid class');
            return FALSE;
        }
        
        return TRUE;
    }
}

So from your controller:
Code:
$this->load->library('product_validation');

$this->product_validation->set_rules(
        'class',
        'Product Class',
        'numeric|xss_clean|valid_product_class'
    );

if ($this->form_validation->run())
{
    // WOOHOO!!!
}

// D'OH!

Hope this helps.
#4

[eluser]behnampmdg3[/eluser]
Thanks TheFuzzy0ne.




Theme © iAndrew 2016 - Forum software by © MyBB