用户
 找回密码
 入住 CI 中国社区
搜索
查看: 5349|回复: 3
收起左侧

[Others] 扩展CI类必须注意的问题

[复制链接]
发表于 2010-9-5 14:30:54 | 显示全部楼层 |阅读模式
本帖最后由 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()设置规则,否则配置文件被全部覆盖!
发表于 2010-9-5 14:56:36 | 显示全部楼层
我看了一下表单验证类的源码,貌似是有作用的,因为每个类库都会自动装载 config 文件,这是 loader 类提供的机制。
楼主不妨贴出你的代码,大家分析一下,呵呵
发表于 2010-9-5 16:21:09 | 显示全部楼层
偶有一个项目,全部验证规则都写在配置文件中,CI172运行无错
发表于 2010-9-14 15:30:13 | 显示全部楼层
晒一晒我的 form_validation 扩展
----------------------------------------------------------------
先新增目录 application/config/form_validation
在 form_validation 末尾新增方法:
PHP复制代码
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
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[2]|max_length[75]'),
            ),
    '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[2]|max_length[75]'),
            array('field' => 'state', 'label' => 'lang:consumer_integral_log_state_field', 'rules' => 'trim|required'),
        )
);
复制代码

测试用例--语言包.php
PHP复制代码
$lang['consumer_integral_log_id_field'] = '消费者积分';
$lang['consumer_integral_log_about_field'] = '描述';
$lang['consumer_integral_log_state_field'] = '状态';
复制代码

....省略...
测试用例--控制器.php
PHP复制代码
$this->load->library('form_validation');
$this->lang->load("语言包");
$this->form_validation->set_config_rules('验证文件名');
if ($this->form_validation->run('insert') == true)
{
///实现逻辑
}
复制代码

评分

参与人数 1威望 +3 收起 理由
lamtin + 3 原创内容

查看全部评分

本版积分规则