第四節視頻:php
表單驗證操做:css
一、載入驗證類html
$this ->load ->library('form_validation');數據庫
二、設置規則app
$this ->form_validation ->set_rules('name值',‘標籤名稱’,‘規則’);框架
三、執行驗證(返回bool值)函數
$this ->form_validation ->run();post
表單驗證輔助函數:ui
一、$this ->load ->helper('form');this
二、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'); } } }