在 CodeIgniter 中扩展类时调用未定义的方法 CI_Form_validation::ci_form_validation()

发布于 2024-12-13 02:24:38 字数 1550 浏览 0 评论 0原文

我想为我的项目添加一些自定义验证功能。我正在使用 CI 2.0.2。我遵循了 www.michaelwales 中描述的所有步骤.com/2010/02/basic-pattern-matching-form-validation-in-codeigniter/

我在 application\libraries 中创建了一个类,如下所示-

<?php if (!defined('BASEPATH')) exit('No direct script access allowed.');
class MY_Form_validation extends CI_Form_validation {

     public function MY_Form_validation() {
        parent::__construct();
      }

             function valid_us_zip()
            {...........}
}//class ends

我在 application\config\form_validation.php 中添加了规则,如下 -

   $config = array(
       'login/sign_up_process' => array(
           array(
                 'field' => 'txt_username_signup',
                 'label' => 'Username',
                 'rules' => 'trim|required|valid_email|xss_clean|prep_for_form'
                                     ),
           array(
                 'field' => 'txt_zip_signup',
                 'label' => 'Zip',
                 'rules' => 'trim|required|valid_us_zip|xss_clean|prep_for_form'
                                     )                                           

                                )
           );

在我的控制器函数中,我添加了代码 -

$this->load->library('form_validation');
echo "Error ::".$this->form_validation->run();

在扩展表单验证类之前,我的验证正在工作,但现在即使我提交空,它也不会给出任何错误形式。我无法解决这个问题:(

I want to add some custom validation functions for my project. I'm using CI 2.0.2. I followed all the steps described at www.michaelwales.com/2010/02/basic-pattern-matching-form-validation-in-codeigniter/

I created a class in application\libraries as follows -

<?php if (!defined('BASEPATH')) exit('No direct script access allowed.');
class MY_Form_validation extends CI_Form_validation {

     public function MY_Form_validation() {
        parent::__construct();
      }

             function valid_us_zip()
            {...........}
}//class ends

I added rule to my application\config\form_validation.php as follows -

   $config = array(
       'login/sign_up_process' => array(
           array(
                 'field' => 'txt_username_signup',
                 'label' => 'Username',
                 'rules' => 'trim|required|valid_email|xss_clean|prep_for_form'
                                     ),
           array(
                 'field' => 'txt_zip_signup',
                 'label' => 'Zip',
                 'rules' => 'trim|required|valid_us_zip|xss_clean|prep_for_form'
                                     )                                           

                                )
           );

In my controller function I've added code as -

$this->load->library('form_validation');
echo "Error ::".$this->form_validation->run();

My validation was working before extending form validation class, but now it does not give any error even though I submit empty form. I'm not able to fix this :(

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

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

发布评论

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

评论(3

沙与沫 2024-12-20 02:24:38

最后,我让它工作了:)

我在问题中解释的所有步骤都是正确的。在 CI2+ 中,正如 swatkins 提到的,我们将扩展核心类文件保留在 application/core 中,库文件保留在 application/library 中。

我将扩展的 MY_Form_validation.php 保留在 application/library 中,发现 $config 没有传递给父级的构造函数。我将其编辑为 -

<?php if (!defined('BASEPATH')) exit('No direct script access allowed.');
class MY_Form_validation extends CI_Form_validation {

 public function MY_Form_validation($config) {
    parent::__construct($config);
  }

         function valid_us_zip()
        {...........}
}//class ends

并对其进行了测试。我的自定义函数被调用,我也收到了错误消息。我希望这对某人有帮助。

Finally, I got it working :)

All the steps I explained in my question are correct. In CI2+, as swatkins has mentioned we are to keep the extended core class files in application/core and library files in application/library.

I kept my extended MY_Form_validation.php in application/library and found that the $config was not passed to the constructor of parent. I edited it as -

<?php if (!defined('BASEPATH')) exit('No direct script access allowed.');
class MY_Form_validation extends CI_Form_validation {

 public function MY_Form_validation($config) {
    parent::__construct($config);
  }

         function valid_us_zip()
        {...........}
}//class ends

And tested it. My custom function was called and I received the error message also. I hope this helps somebody.

酒解孤独 2024-12-20 02:24:38

在 CI2+ 中,您需要将这些扩展核心的新库放在 core 文件夹中,而不是放在 libraries 中。

In CI2+, you need to put these new libraries that extend the core in the core folder, not in libraries.

指尖上的星空 2024-12-20 02:24:38

在您的应用程序/库目录中创建文件名:
MY_Form_validation.php

然后在你的班级

代码中:

class MY_Form_validation extends CI_Form_validation

{
 function My_Form_validation()
   {
       parent::__construct();
   }

}

Make file name in your application/library directory:
MY_Form_validation.php

then in your class

Code:

class MY_Form_validation extends CI_Form_validation

{
 function My_Form_validation()
   {
       parent::__construct();
   }

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