后盾网-CI框架实例教程-马振宇 - 学习笔记(4)

第四节视频:

  表单验证操作:

    1、载入验证类

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

    2、设置规则

      $this ->form_validation ->set_rules('name值',‘标签名称’,‘规则’);

    3、执行验证(返回bool值)

      $this ->form_validation ->run();

  表单验证辅助函数:

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

    2、set_value('name')  //充填数据(刷新时保留原来数据)

      form_error('name','<span>','</span>')  //显示错误

      set_select()  //(刷新时保留原来数据)

      set_checkbox()  //(刷新时保留原来数据)

      set_radio()  //(刷新时保留原来数据)

 

  举栗子:

  注意:建立文章管理控制器专门管理文章,在建立控制器时,一个文章管理控制器就管相关的,管理文章的增加、修改、删除等等一系列相关的操作,总的来说,就是管理文章;

    在application/controllers/admin/下新建文章管理控制器article.php

      class Article extends CI_Controller{
        //发表模板显示
        public function send_article(){
          $this ->load ->view('admin/article.html');
        }
      }

    在application/views/admin/下创建文章发表模板article.html

    在application/controllers/admin/下的article控制器里新建发表文章动作方法:

      //发表文章动作
      public function send(){
        //载入表单类
        $this ->load ->library('form_validation');
        //配置规则
        $this ->form_validation ->set_rules('title','文章标题','required|min_length[5]');//标题不能为空,至少为5个
        //验证规则
        $status = $this ->form_validation ->run();
        var_dump($status);
      }

      在application/views/admin/article.html中,form表单提交地址为:

      <form action="<?php echo site_url('/admin/article/send')?>" method="post" enctype="multipart/form-data">

      注意:在ci框架中,所有链接和表单提交的地址,都是提交到控制器下的某一方法,由该方法决定下面所进行的动作;

 

 

  附:application/views/admin/article.html代码: 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="<?php echo base_url().'style/admin/css/public.css'?>">
    <title>Title</title>
</head>
<body>
<form action="<?php echo site_url('/admin/article/send')?>" method="post" enctype="multipart/form-data">/* enctype="multipart/form-data 文件上传时使用 */
    <table class = "table">
        <tr>
            <td colspan="10" class="th">发表文章</td>
        </tr>
        <tr>
            <td>标题</td>
            <td><input type="text" name="title" value="<?php echo set_value('title') ?>">/* value = "<?php echo set_value('title') ?>" 刷新时保留原来数据*/
                <?php echo form_error('title','<span>','</td>') ?>/* 该行代码为:表单验证辅助函数,需先在控制器中载入辅助函数:$this ->load ->helper('form');汉化需ci框架汉化包*/
            </td>
        </tr>
        <tr>
            <td>类型</td>
            <td>
                <input type="radio"> 普通
                <input type="radio"> 热门
            </td>
        </tr>
        <tr>
            <td>栏目</td>
            <td>
                <select name="" id="">
                    <option value="">情感</option>
                    <option value="">生活</option>
                </select>
            </td>
        </tr>
        <tr>
            <td>缩略图</td>
            <td>
                <input type="file">
            </td>
        </tr>
        <tr>
            <td>摘要</td>
            <td>
                <textarea name="" id="" style="width: 550px;height: 160px;"></textarea>
            </td>
        </tr>
        <tr>
            <td>内容</td>
            <td>
                <textarea name="" id="" style="width: 550px;height: 160px;"></textarea>
            </td>
        </tr>
        <tr>
            <td colspan="10"><input type="submit" class="input_button" value="发布"></td>
        </tr>

    </table>
</form>
</body>
</html>

 

 

  附:application/controllers/admin/article.php

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/10/7
 * Time: 9:08
 */
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Article extends CI_Controller{

    //发表模板显示
    public function send_article(){
        //加载表单验证辅助函数
        $this ->load ->helper('form');
        $this ->load ->view('admin/article.html');
    }

    //发表文章动作
    public  function send(){
        //载入表单类
        $this ->load ->library('form_validation');
        //配置规则
        $this ->form_validation ->set_rules('title','文章标题','required|min_length[5]');//标题不能为空,至少为5个
        //验证规则
        $status = $this ->form_validation ->run();

        if($status){
            echo '数据库操作';
        }else{
            $this ->load ->helper('form');
            $this ->load ->view('admin/article.html');
        }


    }
}

 

posted @ 2017-10-07 12:27  一纸流年  阅读(851)  评论(0编辑  收藏  举报