开发者

Problem with my own libraries and check forms. Codeigniter

I have a problem, everything was working but then I tried to put my functions in my own libraries (to use them in different controllers) and it doesn't work.

I have SIGNUP controller with this:

$this->load->library('Check_functions');        
// We check the form
$return_verif_form_signup = $this->check_functions->verif_form_signup($language);

which calls my librarie Check_functions:

class Check_functions {
pu开发者_开发问答blic function verif_form_signup($language) {
if ($this->input->post()){
// Verification rules
$this->form_validation->set_rules('name', 'lang:name', 'trim|required|xss_clean');
....

if ($this->form_validation->run($this)) {
                extract($_POST);
...
...
}

But I get the error: Fatal error: Call to a member function post() on a non-object

Does anyone know how I could fix it?

thanks!

EDIT:

I have found the problem, the callback function is not called. If I replace callback_free_email by REQUIRED and I don't enter an email, my form is not submitted, so it's okay. But if I have the following code, my form is always submitted. So the callback function is never called...

This is my code (i'm using HMVC):

class Check_functions {

private $CI;
public function __construct(){
      $this->CI =& get_instance();
}

public function verif_form_signup($language) {
   if ($this->CI->input->post()){
      $this->CI->form_validation->set_rules('name', 'lang:field_name', 'trim|required|min_length[3]|max_length[25]|xss_clean');
      $this->CI->form_validation->set_rules('email_signup', 'lang:field_email', 'callback_free_email');
...//other rules

        if ($this->CI->form_validation->run($this->CI)) {
         .....
        }
    }
}

public function free_email($str) {
        return FALSE; // I have temporarly set that so I see if my function is called
    }
}

I have a file called MY_Form_validation.php as suggested here: http://codeigniter.com/forums/viewthread/143057/#769347

class MY_Form_validation extends CI_Form_validation{

    function run($module = '', $group = ''){
        (is_object($module)) AND $this->CI = &$module;
            return parent::run($group);
    }

}

I really don't know what's wrong... why my callback function is not called?

Thank you for your help!


when you are writing libraries, you have to manually grab the Codeigniter instance like this

$CI =& get_instance();

then you would use $CI where you would normally use $this to interact with loaded codeigniter resources

so...

instead of

$this->input->post();

you would write

$CI->input->post();

Docs explain it here http://codeigniter.com/user_guide/general/creating_libraries.html

EXAMPLE LIBRARY STRUCTURE

class Examplelib {

    // declare your CI instance class-wide private
    private $CI;

    public function __construct()
    {
        // get the CI instance and store it class wide
        $this->CI =& get_instance();
    }

    public function lib_function()
    {
        // use it here
        $this->CI->db->etc()
    }

    public function another_func()
    {
        // and here
        $this->CI->input->post();
    }

}


I finally found a workaround, instead of using the callback in my rule, I do a test later and call a verification function. It’s works well like that. Thanks for your help!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜