扩展CI类必须注意的问题
本帖最后由 ares333 于 2010-9-5 18:04 编辑Form_validation类将一系列验证规则保存到一个配置文件无论如何不起作用,看样子只能使用config类手动加载了
找到问题的原因了,因为我扩展了form_validation类,但是构造函数没有加参数,所以导致配置文件不能自动被加载,看样子很多人是时候检查一下自己的扩展了
class X_Form_validation extends CI_Form_validation{
function __construct($rules = array()){
parent::__construct($rules);
}
...
注意参数类型要与父类保持一致
来句题外话
使用form_validation通过配置文件保存规则时,不能再使用set_rules()设置规则,否则配置文件被全部覆盖! 我看了一下表单验证类的源码,貌似是有作用的,因为每个类库都会自动装载 config 文件,这是 loader 类提供的机制。
楼主不妨贴出你的代码,大家分析一下,呵呵 偶有一个项目,全部验证规则都写在配置文件中,CI172运行无错 晒一晒我的 form_validation 扩展
----------------------------------------------------------------
先新增目录 application/config/form_validation
在 form_validation 末尾新增方法:
function set_config_rules($file_name)
{
$config = array();
if (file_exists(APPPATH . 'config/form_validation/' . strtolower($file_name) . EXT))
{
include_once (APPPATH . 'config/form_validation/' . strtolower($file_name) . EXT);
} else
{
if (file_exists(APPPATH . 'config/form_validation/' . ucfirst(strtolower($file_name)) . EXT))
{
include_once (APPPATH . 'config/form_validation/' . ucfirst(strtolower($file_name)) . EXT);
}
}
$this->_config_rules = $config;
}
测试用例---验证文件名.php
$config = array(
'insert' => array(
array('field' => 'user_id', 'label' => 'lang:consumer_integral_log_user_id_field', 'rules' => 'trim|required|numeric'),
array('field' => 'about', 'label' => 'lang:consumer_integral_log_about_field', 'rules' => 'trim|required|min_length|max_length'),
),
'update' => array(
array('field' => 'user_id', 'label' => 'lang:consumer_integral_log_user_id_field', 'rules' => 'trim|required|numeric'),
array('field' => 'about', 'label' => 'lang:consumer_integral_log_about_field', 'rules' => 'trim|required|min_length|max_length'),
array('field' => 'state', 'label' => 'lang:consumer_integral_log_state_field', 'rules' => 'trim|required'),
)
);
测试用例--语言包.php
$lang['consumer_integral_log_id_field'] = '消费者积分';
$lang['consumer_integral_log_about_field'] = '描述';
$lang['consumer_integral_log_state_field'] = '状态';
....省略...
测试用例--控制器.php
$this->load->library('form_validation');
$this->lang->load("语言包");
$this->form_validation->set_config_rules('验证文件名');
if ($this->form_validation->run('insert') == true)
{
///实现逻辑
}
页:
[1]