Table of Contents
Yii CModel中rules验证规则[转],yiicmodel
Yii rules 规则 自定义写法
yii 中的验证码出不来是什问题
Home php教程 php手册 Yii CModel中rules验证规则[转],yiicmodel

Yii CModel中rules验证规则[转],yiicmodel

Jun 13, 2016 am 09:27 AM
yii

Yii CModel中rules验证规则[转],yiicmodel

 

<span>array</span><span>(

</span><span>array</span>(&lsquo;username&rsquo;, &lsquo;required&rsquo;),
 <span>array</span>(&lsquo;username&rsquo;, &lsquo;length&rsquo;, &lsquo;<span>min</span>&rsquo;=>3, &lsquo;<span>max</span>&rsquo;=>12),
 <span>array</span>(&lsquo;password&rsquo;, &lsquo;compare&rsquo;, &lsquo;compareAttribute&rsquo;=>&rsquo;password2&prime;, &lsquo;on&rsquo;=>&rsquo;register&rsquo;),
 <span>array</span>(&lsquo;password&rsquo;, &lsquo;authenticate&rsquo;, &lsquo;on&rsquo;=>&rsquo;login&rsquo;),
  <span>array</span>(&lsquo;Price&rsquo;,&rsquo;numerical&rsquo;, &lsquo;integerOnly&rsquo;=><span>true</span>),<span>
);
</span><span>public</span> <span>function</span><span> rules()
{
  </span><span>return</span> <span>array</span><span>(
      </span><span>array</span>(&lsquo;title, content, status&rsquo;, &lsquo;required&rsquo;),
      <span>array</span>(&lsquo;title&rsquo;, &lsquo;length&rsquo;, &lsquo;<span>max</span>&rsquo;=>128),
      <span>array</span>(&lsquo;status&rsquo;, &lsquo;in&rsquo;, &lsquo;<span>range</span>&rsquo;=><span>array</span>(1,2,3)),
      <span>array</span>(&lsquo;tags&rsquo;, &lsquo;match&rsquo;, &lsquo;pattern&rsquo;=>&rsquo;/^[\w\s,]+$/&rsquo;,<span>
          &lsquo;message&rsquo;</span>=>&rsquo;Tags can only contain word characters.&rsquo;),
      <span>array</span>(&lsquo;tags&rsquo;, &lsquo;normalizeTags&rsquo;),
      <span>array</span>(&lsquo;title, status&rsquo;, &lsquo;safe&rsquo;, &lsquo;on&rsquo;=>&rsquo;search&rsquo;),<span>
  );
}</span>
Copy after login

 

预定义完整列表: 
  • boolean : CBooleanValidator 的别名, 确保属性的值是CBooleanValidator::trueValue 或CBooleanValidator::falseValue .

  • captcha : CCaptchaValidator 的别名,确保了特性的值等于 CAPTCHA 显示出来的验证码.

  • compare : CCompareValidator 的别名, 确保了特性的值等于另一个特性或常量.

  • email : CEmailValidator 的别名,确保了特性的值是一个有效的电邮地址.

  • default : CDefaultValueValidator 的别名, 为特性指派了一个默认值.

  • exist : CExistValidator 的别名, 确保属性值存在于指定的数据表字段中.

  • file : CFileValidator 的别名, 确保了特性包含了一个上传文件的名称.

  • filter : CFilterValidator 的别名, 使用一个filter转换属性.

  • in : CRangeValidator 的别名, 确保了特性出现在一个预订的值列表里.

  • length : CStringValidator 的别名, 确保了特性的长度在指定的范围内.

  • match : CRegularExpressionValidator 的别名, 确保了特性匹配一个正则表达式.

  • numerical : CNumberValidator 的别名, 确保了特性是一个有效的数字.

  • required : CRequiredValidator 的别名, 确保了特性不为空.

  • type : CTypeValidator 的别名, 确保了特性为指定的数据类型.

  • unique : CUniqueValidator 的别名, 确保了特性在数据表字段中是唯一的.

  • url : CUrlValidator 的别名, 确保了特性是一个有效的路径 

yii验证rulesit 分类: Yii yii的rules验证 cValidator主要属性 attributes ,builtInValidators,enableClientValidation,message,on,safe,skipOnError

经常用到的属性有 attributes,builtInvalidators,message,on这四个

下面是对应的验证类

required: CRequiredValidator

filter: CFilterValidator

match: CRegularExpressionValidator

email: CEmailValidator

url: CUrlValidator

unique: CUniqueValidator

compare: CCompareValidator

length: CStringValidator

in: CRangeValidator

numerical: CNumberValidator

captcha: CCaptchaValidator

type: CTypeValidator

file: CFileValidator

default: CDefaultValueValidator

exist: CExistValidator

boolean: CBooleanValidator

date: CDateValidator

safe: CSafeValidator

unsafe: CUnsafeValidator

1、CRequiredValidator – 必须值验证属性

requiredValue-mixed-所需的值

strict-boolean-是否比较严格

实例: array(‘username’, ‘required’), 不能为空

array(‘username’, ‘required’, ‘requiredValue’=>’lh’,’message’=> ‘usernmae must be lh’), 这个值必须为lh,如果填其他值还是会验证不过

array(‘username’, ‘required’, ‘requiredValue’=>’lh’, ‘strict’=>true), 严格验证 还可以在后面加 ‘message’=>”,’on’=>这些

2、CFilterValidator 过滤验证属性

filter – 方法名 (调用用户自己定义的函数)

实例:

array(‘username’, ‘test’) function test() { $username = $this->username; if($username != ‘lh’){ $this->addError(‘username’, ‘username must be lh’); } }

使用这个方法如果你还在array里面写message=>”,给出的提示信息还是你的test里面的。也就是以test里面的错误信息为准

3、CRegularExpressionValidator -

正则验证属性allowEmpty – 是否为空(默认true)

not-是否反转的验证逻辑(默认false) pattern – 正则表达式匹配实例:

// 匹配a-z array(‘username’, ‘match’, ‘allowEmpty’=>true, ‘pattern’=>’/[a-z]/i’,’message’=>’必须为字母’),

// 匹配不是a-z array(‘username’, ‘match’, ‘allowEmpty’=>true, ‘not’=>true, ‘pattern’=>’/[a-z]/i’,’message’=>’必须不是字母’),

4、CEmailValidator –邮箱验证属性:

allowEmpty – 是否为空

allowName – 是否允许在电子邮件地址的名称

checkMx – 是否检查电子邮件地址的MX记录

checkPort – 是否要检查端口25的电子邮件地址

fullPattern – 正则表达式,用来验证电子邮件地址与名称的一部分

pattern – 正则表达式,

用来验证的属性值实例: array(‘username’, ‘email’, ‘message’=>’必须为电子邮箱’, ‘pattern’=>’/[a-z]/i’),

5、CUrlValidator – url验证属性:

allowEmpty – 是否为空

defaultScheme – 默认的URI方案

pattern – 正则表达式

validSchemes – 清单应视为有效的URI计划。

实例:

array(‘username’, ‘url’, ‘message’=>’must url’),

array(‘username’, ‘url’, ‘defaultScheme’=>’http://www.baidu.com’),

6、CUniqueValidator – 唯一性验证属性:

allowEmpty – 是否为空

attributeName – 属性名称

caseSensitive – 区分大小写

className – 类名

criteria – 额外的查询条件

实例:

array(‘username’, ‘unique’, ‘message’=>’该记录存在’),

array(‘username’, ‘unique’, ‘caseSensitive’=>false, ‘message’=>’该记录存在’),

7、CCompareValidator – 比较验证属性:

allowEmpty – 是否为空

compareAttribute – 需要比较的属性

compareValue -比较的值

operator – 比较运算符

strict – 严格验证(值和类型都要相等)

实例: // 与某个值比较 array(‘username’, ‘compare’, ‘compareValue’=>’10′, ‘operator’=>’>’, ‘message’=>’必须大于10′),

// 与某个提交的属性比较 array(‘username’, ‘compare’, ‘compareAttribute’=& gt;’password’, ‘operator’=>’>’, ‘message’=>’必须大于password’),

8、CStringValidator – 字符串验证属性:

allowEmpty – 是否为空

encoding – 编码

is – 确切的长度

max – 最大长度

min – 最小长度

tooLong – 定义值太大的错误

tooShort – 定义最小长度的错误

实例: array(‘username’, ‘length’, ‘max’=>10, ‘min’=>5, ‘tooLong’=>’太长了’, ‘tooShort’=>’太短了’),

array(‘username’, ‘length’, ‘is’=>5, ‘message’=>’长度必须为5′),

9、CRangeValidator – 在某个范围内属性:

allowEmpty – 是否为空

not – 是否反转的验证逻辑。

range – array范围

strict – 严格验证(类型和值都要一样)

实例: array(‘username’, ‘in’, ‘range’=>array(1,2,3,4,5), ‘message’=>’must in 1 2 3 4 5′),

array(‘username’, ‘in’, ‘not’=>true, ‘range’=>array(1,2,3,4,5), ‘message’=>’must not in 1 2 3 4 5′),

10、CNumberValidator – 数字验证属性:

allowEmpty – 是否为空

integerOnly – 整数

integerPattern – 正则表达式匹配整数

max – 最大值

min – 最小值

numberPattern – 匹配号码

tooBig – 值太大时的错误提示

tooSmall – 值太小时的错误提示

实例: array(‘username’, ‘numerical’, ‘integerOnly’=>true, ‘message’=>’must be int’),

array(‘username’, ‘numerical’, ‘integerOnly’=>true, ‘message’=>’must be int’, ‘max’=>100, ‘min’=>10, ‘tooBig’=>’is too big’, ‘tooSmall’=>’is too small’),

11、CCaptchaValidator – 验证码验证属性:

allowEmpty – 是否为空

caseSensitive – 区分大小写

12、CTypeValidator – 类型验证属性:

allowEmpty – 是否为空

dateFormat – 日期应遵循的格式模式(‘MM/dd/yyyy’)

datetimeFormat – 日期时间应遵循的格式模式(‘MM/dd/yyyy hh:mm’)

timeFormat – 时间应遵循的格式模式(‘hh:mm’)

type – 类型 ‘string’, ‘integer’, ‘float’, ‘array’, ‘date’, ‘time’ and ‘datetime’

实例: array(‘username’, ‘type’, ‘dateFormat’=>’MM/dd/yyyy’, ‘type’=>’date’),

13、CFileValidator – 文件验证属性:

allowEmpty – 是否为空

maxFiles – 最大文件数

maxSize – 文件的最大值

minSize – 最小值

tooLarge – 太大时的错误信息

tooMany – 太多时的错误信息

tooSmall – 太小时的错误信息

types – 允许的文件扩展名

wrongType – 扩展名错误时给出的错误信息

14、CDefaultValueValidator – 默认值属性:

setOnEmpty – 设置为空

value – 默认值

实例: array(‘username’, ‘default’, ‘setOnEmpty’=>true, ‘value’=>’lh’),

15、CExistValidator – 是否存在属性:

allowEmpty = 是否为空

attributeName – 属性名称

className – 类名

criteria – 标准

16、CBooleanValidator – 布尔类型验证属性:

allowEmpty – 是否为空

falseValue – 错误状态的值

strict – 严格验证

trueValue – 真实状态的值

实例: array(‘username’, ‘boolean’, ‘trueValue’=>1, ‘falseValue’=>-1, ‘message’=>’the value must be 1 or -1′),

17、CDateValidator – 日期验证属性:

allowEmpty – 是否为空

format – 日期值应遵循的格式模式

timestampAttribute – 接收解析结果的属性名称

实例: array(‘username’, ‘date’, ‘format’=>’MM-dd-yyyy’,’message’=>’must be MM-dd-yyyy’),

Yii rules 规则 自定义写法

yxmhero1989.blog.163.com/...61864/
 

yii 中的验证码出不来是什问题

本文描述yii验证码的实现,仅仅是笔者应用的一个小例子,网上也有,总结一下,希望帮助到有需要的Yii爱好者。1、笔者用的是用户登录,所以在sitecontroller中加以下代码: public function actions()
{
return array(
// captcha action renders the CAPTCHA image displayed on the contact page
‘captcha’=>array(
‘class’=>’CCaptchaAction’,
‘backColor’=>0xFFFFFF, //背景颜色
‘minLength’=>4, //最短为4位
‘maxLength’=>4, //是长为4位
‘transparent’=>true, //显示为透明,当关闭该选项,才显示背景颜色
),
);
}
2、在表单文件中加如下代码(视图文件如login.php):

labelEx($model,’verifyCode’); ?>


widget(‘CCaptcha’); ?>
textField($model,’verifyCode’); ?>

输入验证码


error($model,’verifyCode’); ?>

3、在Loginform模型(LoginForm.php)中 加入如下代码,主要是添加属性字段,否则会报错(不存在的属性 ) public $username;
public $password;
public $verifyCode;
public $rememberMe;
private $_identity; 通过上面的操作实际上我们已经可以看到验证码了,但是在操作的时候我们会发现不输入验证码仍然可以,原因在于我们还没有指定验证是必须的,在LoginForm.php中加上array(‘verifyCode’,'required’)来指定必须,这时我们如果遗漏验证码,将会如下图所示:
本文描述yii验证码的实现,仅仅是笔者应用的一个小例子,网上也有,总结一下,希望帮助到有需要的Yii爱好者。1、笔者用的是用户登录,所以在sitecontroller中加以下代码......余下全文>>
 

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1675
14
PHP Tutorial
1278
29
C# Tutorial
1257
24
Yii2 vs Phalcon: Which framework is better for developing graphics rendering applications? Yii2 vs Phalcon: Which framework is better for developing graphics rendering applications? Jun 19, 2023 am 08:09 AM

In the current information age, big data, artificial intelligence, cloud computing and other technologies have become the focus of major enterprises. Among these technologies, graphics card rendering technology, as a high-performance graphics processing technology, has received more and more attention. Graphics card rendering technology is widely used in game development, film and television special effects, engineering modeling and other fields. For developers, choosing a framework that suits their projects is a very important decision. Among current languages, PHP is a very dynamic language. Some excellent PHP frameworks such as Yii2, Ph

How to use PHP framework Yii to develop a highly available cloud backup system How to use PHP framework Yii to develop a highly available cloud backup system Jun 27, 2023 am 09:04 AM

With the continuous development of cloud computing technology, data backup has become something that every enterprise must do. In this context, it is particularly important to develop a highly available cloud backup system. The PHP framework Yii is a powerful framework that can help developers quickly build high-performance web applications. The following will introduce how to use the Yii framework to develop a highly available cloud backup system. Designing the database model In the Yii framework, the database model is a very important part. Because the data backup system requires a lot of tables and relationships

How to use Yii3 framework in php? How to use Yii3 framework in php? May 31, 2023 pm 10:42 PM

As the Internet continues to develop, the demand for web application development is also getting higher and higher. For developers, developing applications requires a stable, efficient, and powerful framework, which can improve development efficiency. Yii is a leading high-performance PHP framework that provides rich features and good performance. Yii3 is the next generation version of the Yii framework, which further optimizes performance and code quality based on Yii2. In this article, we will introduce how to use Yii3 framework to develop PHP applications.

Data query in Yii framework: access data efficiently Data query in Yii framework: access data efficiently Jun 21, 2023 am 11:22 AM

The Yii framework is an open source PHP Web application framework that provides numerous tools and components to simplify the process of Web application development, of which data query is one of the important components. In the Yii framework, we can use SQL-like syntax to access the database to query and manipulate data efficiently. The query builder of the Yii framework mainly includes the following types: ActiveRecord query, QueryBuilder query, command query and original SQL query

Symfony vs Yii2: Which framework is better for developing large-scale web applications? Symfony vs Yii2: Which framework is better for developing large-scale web applications? Jun 19, 2023 am 10:57 AM

As the demand for web applications continues to grow, developers have more and more choices in choosing development frameworks. Symfony and Yii2 are two popular PHP frameworks. They both have powerful functions and performance, but when faced with the need to develop large-scale web applications, which framework is more suitable? Next we will conduct a comparative analysis of Symphony and Yii2 to help you make a better choice. Basic Overview Symphony is an open source web application framework written in PHP and is built on

Yii2 vs Symfony: Which framework is better for API development? Yii2 vs Symfony: Which framework is better for API development? Jun 18, 2023 pm 11:00 PM

With the rapid development of the Internet, APIs have become an important way to exchange data between various applications. Therefore, it has become increasingly important to develop an API framework that is easy to maintain, efficient, and stable. When choosing an API framework, Yii2 and Symfony are two popular choices among developers. So, which one is more suitable for API development? This article will compare these two frameworks and give some conclusions. 1. Basic introduction Yii2 and Symfony are mature PHP frameworks with corresponding extensions that can be used to develop

Yii2 Programming Guide: How to run Cron service Yii2 Programming Guide: How to run Cron service Sep 01, 2023 pm 11:21 PM

If you're asking "What is Yii?" check out my previous tutorial: Introduction to the Yii Framework, which reviews the benefits of Yii and outlines what's new in Yii 2.0, released in October 2014. Hmm> In this Programming with Yii2 series, I will guide readers in using the Yii2PHP framework. In today's tutorial, I will share with you how to leverage Yii's console functionality to run cron jobs. In the past, I've used wget - a web-accessible URL - in a cron job to run my background tasks. This raises security concerns and has some performance issues. While I discussed some ways to mitigate the risk in our Security for Startup series, I had hoped to transition to console-driven commands

Yii with Docker: Containerizing and Deploying Your Applications Yii with Docker: Containerizing and Deploying Your Applications Apr 02, 2025 pm 02:13 PM

The steps to containerize and deploy Yii applications using Docker include: 1. Create a Dockerfile and define the image building process; 2. Use DockerCompose to launch Yii applications and MySQL database; 3. Optimize image size and performance. This involves not only specific technical operations, but also understanding the working principles and best practices of Dockerfile to ensure efficient and reliable deployment.

See all articles