我自己的图书馆和检查表格有问题。代码点火器

发布于 2024-11-28 18:06:03 字数 2179 浏览 2 评论 0原文

我有一个问题,一切正常,但后来我尝试将我的函数放入我自己的库中(以在不同的控制器中使用它们),但它不起作用。

我有这样的 SIGNUP 控制器:

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

它调用我的库 Check_functions:

class Check_functions {
public 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);
...
...
}

但我收到错误: 致命错误:在非对象上调用成员函数 post()

有谁知道我如何修复它?

谢谢!

编辑:

我发现了问题,回调函数没有被调用。如果我将 callback_free_email 替换为 REQUIRED 并且没有输入电子邮件,则我的表单不会提交,所以没关系。 但如果我有以下代码,我的表单总是会提交。所以回调函数永远不会被调用...

这是我的代码(我正在使用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
    }
}

我有一个名为MY_Form_validation.php的文件,如下所示: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 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 {
public 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

樱桃奶球 2024-12-05 18:06:03

当您编写库时,您必须像这样手动获取 Codeigniter 实例

$CI =& get_instance();

,然后您将使用 $CI (通常使用 $this 与加载的 codeigniter 资源进行交互

) ...

而不是

$this->input->post();

您在此处编写

$CI->input->post();

文档进行解释 http://codeigniter.com/user_guide/general/creating_libraries.html

示例库结构

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();
    }

}

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();
    }

}
囚你心 2024-12-05 18:06:03

我终于找到了一个解决方法,我没有在规则中使用回调,而是稍后进行测试并调用验证函数。
就这样运作得很好。
感谢您的帮助!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文