PHP CI框架中的表单验证规则

7. 载入框架中的类(此处以表单验证为例)

    public function index()

    {

        $this->load->helper('form');   //加载表单验证辅助函数

        $this->load->view("index/home.html");

    }

 

    public function send()

    {

        //载入表单验证类

        $this->load->library("form_validation");

        //设置规则

        $this->form_validation->set_rules('title', '标题', 'required|min_length[5]');

//此处传递的三个参数,”title”是需要验证的表单的name,”标题”是提示语,'required|min_length[5]'是验证规则,使用|分割,可以写入多个规则

$this->form_validation->set_rules(‘cid’, '栏目', 'required|min_length[5]');

//设置多个表单验证

        //执行验证

        $status = $this->form_validation->run();  //验证结果为bool值

        if ($status) {

            echo "数据库操作";

        } else {

            $this->load->helper('form');

            $this->load->view("index/home.html");  //不通过加载视图,返回提示信息

        }

}

 

 

设置数组形式传递验证规则:

新建文件:cation/config/form_vadication.php  (必须使用这个文件名)

设置参数

$config=array(

‘test’ =>array(

array(

                         ‘field’=> ‘title’,

                        ‘label’=>’标题’,

                        ‘rules’=>’ required|min_length[5]’

                ),

                array(

                       ‘field’=> ‘cid’,

                        ‘label’=>’栏目’,

                         ‘rules’=>’ required|min_length[5]’

)

)

)

在表单验证的方法中传入该变量

$this->form_validation->run(“test”);

posted @   G1733  阅读(559)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示