在 CodeIgniter 中扩展类时调用未定义的方法 CI_Form_validation::ci_form_validation()
我想为我的项目添加一些自定义验证功能。我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
最后,我让它工作了:)
我在问题中解释的所有步骤都是正确的。在 CI2+ 中,正如 swatkins 提到的,我们将扩展核心类文件保留在 application/core 中,库文件保留在 application/library 中。
我将扩展的 MY_Form_validation.php 保留在 application/library 中,发现 $config 没有传递给父级的构造函数。我将其编辑为 -
并对其进行了测试。我的自定义函数被调用,我也收到了错误消息。我希望这对某人有帮助。
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 -
And tested it. My custom function was called and I received the error message also. I hope this helps somebody.
在 CI2+ 中,您需要将这些扩展核心的新库放在
core
文件夹中,而不是放在libraries
中。In CI2+, you need to put these new libraries that extend the core in the
core
folder, not inlibraries
.在您的应用程序/库目录中创建文件名:
MY_Form_validation.php
然后在你的班级
代码中:
Make file name in your application/library directory:
MY_Form_validation.php
then in your class
Code: