CodeIgniter 允许你为单个表单域创建多个验证规则,按顺序层叠在一起, 你也可以同时对表单域的数据进行预处理。要设置验证规则, 可以使用 set_rules() 方法:

$this->form_validation->set_rules();

上面的方法有 三个 参数:

  1. 第一个参数,表单域名 - 就是你给表单域取的那个名字。
  2. 第二个参数,表单域的 "人性化" 名字,它将被插入到错误信息中。例如, 如果你有一个表单域叫做 “user” ,你可能会给它一个人性化的名字叫做 “用户名” 。
  3. 第三个参数,为此表单域设置的验证规则。
  4. (可选的)当此表单域设置自定义的错误信息,如果没有设置该参数,将使用默认的。

注解

如果你想让表单域的名字保存在一个语言文件里,请参考 翻译表单域名称

下面是个例子,在你的控制器(Form.php)中紧接着验证初始化函数之后,添加这段代码:

$this->form_validation->set_rules('username', 'Username', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');
$this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
$this->form_validation->set_rules('email', 'Email', 'required');

你的控制器现在看起来像这样:

<?php

class Form extends CI_Controller {

    public function index()
{
$this->load->helper(array('form', 'url')); $this->load->library('form_validation'); $this->form_validation->set_rules('username', 'Username', 'required');
$this->form_validation->set_rules('password', 'Password', 'required',
array('required' => 'You must provide a %s.')
);
$this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
$this->form_validation->set_rules('email', 'Email', 'required'); if ($this->form_validation->run() == FALSE)
{
$this->load->view('myform');
}
else
{
$this->load->view('formsuccess');
}
}
}

现在如果你不填写表单就提交,你将会看到错误信息。如果你填写了所有的表单域并提交,你会看到成功页。

注解

当出现错误时表单页将重新加载,所有的表单域将会被清空,并没有被重新填充。 稍后我们再去处理这个问题。

CI设置表单验证规则的更多相关文章

  1. jquery.validate.js使用之自定义表单验证规则

    jquery.validate.js使用之自定义表单验证规则,下面列出了一些常用的验证法规则 jquery.validate.js演示查看 jquery validate强大的jquery表单验证插件 ...

  2. Yii 表单验证规则---总结

    Filter: 过滤,'filter'=>'trim',表示去空格 Required:必须的,表示不能为空 Match: 匹配正则,需要和pattern一起使用,定义正则表达式,'pattern ...

  3. YII 表单验证规则

    官方文档:http://www.yiichina.com/guide/form.model 类参考手册:http://www.yiichina.com/api/CValidatorhttp://www ...

  4. Element表单验证规则

    一.简单的逻辑验证使用方法: 方法步骤: 1.在html中给el-form增加 :rules="rules" 2.html中在el-form-item 中增加属性 prop=&qu ...

  5. iview必备技能一、表单验证规则

    iView表单组件使用async-validator验证器对表单域中数据进行验证,给Form 设置属性 rules,同时给需要验证的 FormItem 设置属性 prop 指向对应字段即可. 完整的验 ...

  6. Model中设置表单验证方法

    Model类里面定义$_validate属性支持的验证因子格式: 格式:array(验证字段,验证规则,错误提示,验证条件,附加规则,验证时间). 验证条件: (1)Model::EXISTS_TO_ ...

  7. jquery.validate.js之自定义表单验证规则

    <html> <head> <meta http-equiv="Content-Type" content="text/html; char ...

  8. vue + element 实现登录注册(自定义表单验证规则)

    注册页包含手机验证码登录和密码的二次验证. 效果如下: 实现代码: <template> <div> <div class="register-wrapper& ...

  9. [转]DWZ表单验证规则一览表(留着自己用)

    <form onsubmit="return validateCallback(this)" class="pageForm" action=" ...

随机推荐

  1. LN : leetcode 413 Arithmetic Slices

    lc 413 Arithmetic Slices 413 Arithmetic Slices A sequence of number is called arithmetic if it consi ...

  2. STM32 (基础语法)笔记

    指针遍历: char *myitoa(int value, char *string, int radix){ int i, d; int flag = 0; char *ptr = string; ...

  3. SugarCRM安装踩雷(一)

    安装SugarCRM前置条件: 1.找对平台.正确版本的安装包 2.APACHE + MYSQL + TOMCAT环境先确保OK 坑1: 进入安装参数设置步骤的MYSQL用户密码——这里根据Mysql ...

  4. raw cannot be resolved or is not a field解决办法

    解决raw文件夹问题 查看左侧项目/res文件夹下是否有raw文件夹,(一定是放到res文件夹下,raw在项目开始创建时候不会自动创建,所以要自己创建)

  5. linux下PPTP Server测试环境搭建

    1.1  服务器软件安装 安裝PPTP  Server 所需的软件: 安装PPTP: sudo apt-get install pptpd PPTP Server的软件安装很简单,只需要安装pptpd ...

  6. CSS3 radio 或checkbox 自定义 样式

    .style-radio {position:relative;width:15px;height:15px;outline:none;} .style-radio:after {position:a ...

  7. 网络基础编程_5.4聊天室-IOCP服务器

    聊天室-IOCP服务器 main 创建完成端口内核对象(CreateIoCompletionPort) 获取核心数并创建线程(GetSystemInfo + CreateThread) 创建套接字并绑 ...

  8. python3安装opencv及电子书籍(百度云)

    不能直接  pip install opencv 正解: pip install opencv-python  记得:请确保网络良好!!!!! (1)这个是我学习的电子书籍:opencv-python ...

  9. ubuntu18.04 frpc安装与自动启动

    1. 下载, 解压 export FRP_VERSION='0.25.3' wget --no-check-certificate https://github.com/fatedier/frp/re ...

  10. CAD参数绘制角度标注(网页版)

    主要用到函数说明: _DMxDrawX::DrawDimAngular 绘制一个角度标注.详细说明如下: 参数 说明 DOUBLE dAngleVertexX 角度标注的顶点的X值 DOUBLE dA ...