您的位置:首页 > 编程语言 > PHP开发

Yii Framework2.0输入验证

2015-03-27 20:15 155 查看
validate() 方法,在幕后为执行验证操作。先看一个简单的例子,例子的代码在《Yii Framework2.0开发教程(2)使用表单Form》。

类ZhyoulunController中函数actionEntry()
,其中有一句$model->validate(),就是来判断输入的名字和电子邮件是否正确。

修改models/EntryForm.php

<?php

namespace <span id="11_nwp" style="padding: 0px; width: auto; height: auto; float: none;"><a target=_blank id="11_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?c=news&cf=1001&ch=0&di=128&fv=11&jk=25533c2a84a5b309&k=app&k0=app&kdi0=0&luki=1&n=10&p=baidu&q=65035100_cpr&rb=0&rs=1&seller_id=1&sid=9b3a5842a3c5325&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1836545&u=http%3A%2F%2Fwww%2Ebubuko%2Ecom%2Finfodetail%2D433353%2Ehtml&urlid=0" target="_blank" mpid="11" style="padding: 0px; color: rgb(51, 51, 51); text-decoration: none;"><span style="padding: 0px; color: rgb(0, 0, 255); width: auto; height: auto;">app</span></a></span>\<span id="12_nwp" style="padding: 0px; width: auto; height: auto; float: none;"><a target=_blank id="12_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?c=news&cf=1001&ch=0&di=128&fv=11&jk=25533c2a84a5b309&k=model&k0=model&kdi0=0&luki=6&n=10&p=baidu&q=65035100_cpr&rb=0&rs=1&seller_id=1&sid=9b3a5842a3c5325&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1836545&u=http%3A%2F%2Fwww%2Ebubuko%2Ecom%2Finfodetail%2D433353%2Ehtml&urlid=0" target="_blank" mpid="12" style="padding: 0px; color: rgb(51, 51, 51); text-decoration: none;"><span style="padding: 0px; color: rgb(0, 0, 255); width: auto; height: auto;">model</span></a></span>s;

use yii\base\Model;

class EntryForm extends Model
{
public $name;
public $email;

public function rules()
{
return [
['name', 'required','message'=>'名称不可缺省'],
['email', 'required','message'=>'邮件不能缺省'],
['email', 'email','message'=>'电子邮件格式不正确'],
];
}
}

可以看到如下表现

都不输入



邮件格式错误



输入都正确



很容易和我们修改后的代码对应。

validate() 方法,在幕后为执行验证操作,进行了以下步骤:

1、通过从 [[yii\base\Model::scenarios()]] 方法返回基于当前 [[yii\base\Model::scenario|场景(scenario)]] 的特性属性列表,算出哪些特性应该进行有效性验证。这些属性被称作active
attributes(激活特性)
d380


2、通过从 [[yii\base\Model::rules()]] 方法返回基于当前 [[yii\base\Model::scenario|场景(scenario)]] 的验证规则列表,这些规则被称作active
rules(激活规则)。

3、用每个激活规则去验证每个与之关联的激活特性。若失败,则记录下对应模型特性的错误信息。

rules()函数的语法

[
// 必须项,用于指定那些模型特性需要通过此规则的验证。
// 对于只有一个特性的情况,可以直接写特性名,而不必用数组包裹。
['attribute1', 'attribute2', ...],

// 必填项,用于指定【规则的类型】。
// 它可以是类名,验证器昵称,或者是验证方法的名称。
'validator',

// 可选项,用于指定在场景(scenario)中,需要启用该规则
// 若不提供,则代表该规则适用于所有场景
// 若你需要提供除了某些特定场景以外的所有其他场景,你也可以配置 "except" 选项
'on' => ['scenario1', 'scenario2', ...],

// 可选项,用于指定对该验证器对象的其他配置选项
'property1' => 'value1', 'property2' => 'value2', ...
]

你可以指定以下的规则类型之一:

核心验证器的昵称,比如 required、in、date,等等。请参考【核心验证器章节】查看完整的核心验证器列表。

模型类中的某个验证方法的名称,或者一个匿名方法。请参考【行内验证器小节】了解更多。

验证器类的名称。请参考【独立验证器小节】了解更多。

一个规则可用于验证一个或多个模型特性,且一个特性可以被一个或多个规则所验证。

核心验证器https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/tutorial-core-validators.md

default

['age', 'default', 'value' => null],// 若 "age" 为空,则将其设为 null
['country', 'default', 'value' => 'USA'],// 若 "country" 为空,则将其设为 "USA"
// 若 "from" 和 "to" 为空,则分别给他们分配自今天起,3 天后和 6 天后的日期。
[['from', 'to'], 'default', 'value' => function ($<span id="6_nwp" style="padding: 0px; width: auto; height: auto; float: none;"><a target=_blank id="6_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?c=news&cf=1001&ch=0&di=128&fv=11&jk=25533c2a84a5b309&k=model&k0=model&kdi0=0&luki=6&n=10&p=baidu&q=65035100_cpr&rb=0&rs=1&seller_id=1&sid=9b3a5842a3c5325&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1836545&u=http%3A%2F%2Fwww%2Ebubuko%2Ecom%2Finfodetail%2D433353%2Ehtml&urlid=0" target="_blank" mpid="6" style="padding: 0px; color: rgb(51, 51, 51); text-decoration: none;"><span style="padding: 0px; color: rgb(0, 0, 255); width: auto; height: auto;">model</span></a></span>, $attribute) {
return date('Y-m-d', strtotime($attribute === 'to' ? '+3 days' :'+6 days'));
}],

trim

['name','trim'],//去掉输入信息的首尾空格


行内验证器

1、以匿名函数形式定义的行内验证器

['name',
function($attribute, $params)
{
if (!ctype_alnum($this->$attribute))
{
$this->addError($attribute, '令牌本身必须包含字母或数字。');
}
}
],




2、以模型方法
validateCountry() 形式定义的行内验证器

<?php

namespace <span id="1_nwp" style="padding: 0px; width: auto; height: auto; float: none;"><a target=_blank id="1_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?c=news&cf=1001&ch=0&di=128&fv=11&jk=25533c2a84a5b309&k=app&k0=app&kdi0=0&luki=1&n=10&p=baidu&q=65035100_cpr&rb=0&rs=1&seller_id=1&sid=9b3a5842a3c5325&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1836545&u=http%3A%2F%2Fwww%2Ebubuko%2Ecom%2Finfodetail%2D433353%2Ehtml&urlid=0" target="_blank" mpid="1" style="padding: 0px; color: rgb(51, 51, 51); text-decoration: none;"><span style="padding: 0px; color: rgb(0, 0, 255); width: auto; height: auto;">app</span></a></span>\<span id="2_nwp" style="padding: 0px; width: auto; height: auto; float: none;"><a target=_blank id="2_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?c=news&cf=1001&ch=0&di=128&fv=11&jk=25533c2a84a5b309&k=model&k0=model&kdi0=0&luki=6&n=10&p=baidu&q=65035100_cpr&rb=0&rs=1&seller_id=1&sid=9b3a5842a3c5325&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1836545&u=http%3A%2F%2Fwww%2Ebubuko%2Ecom%2Finfodetail%2D433353%2Ehtml&urlid=0" target="_blank" mpid="2" style="padding: 0px; color: rgb(51, 51, 51); text-decoration: none;"><span style="padding: 0px; color: rgb(0, 0, 255); width: auto; height: auto;">model</span></a></span>s;

use yii\base\Model;

class EntryForm extends Model
{
public $name;
public $email;

public function rules()
{
return [
['name', 'required','message'=>'名称不可缺省'],
['email', 'required','message'=>'<span id="3_nwp" style="padding: 0px; width: auto; height: auto; float: none;"><a target=_blank id="3_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?c=news&cf=1001&ch=0&di=128&fv=11&jk=25533c2a84a5b309&k=%D3%CA%BC%FE&k0=%D3%CA%BC%FE&kdi0=0&luki=3&n=10&p=baidu&q=65035100_cpr&rb=0&rs=1&seller_id=1&sid=9b3a5842a3c5325&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1836545&u=http%3A%2F%2Fwww%2Ebubuko%2Ecom%2Finfodetail%2D433353%2Ehtml&urlid=0" target="_blank" mpid="3" style="padding: 0px; color: rgb(51, 51, 51); text-decoration: none;"><span style="padding: 0px; color: rgb(0, 0, 255); width: auto; height: auto;">邮件</span></a></span>不能缺省'],
['email', 'email','message'=>'电子邮件格式不正确'],

//						['name',
//							function($attribute, $params)
//							{
//                if (!ctype_alnum($this->$attribute))
//								{
//                    $this->addError($attribute, '令牌本身必须包含字母或数字。');
//                }
//							}
//						],

['name', 'validateCountry'],
];
}

public function validateCountry($attribute, $params)
{
if (!in_array($this->$attribute, ['呵呵', '嘿嘿'])) {
$this->addError($attribute, '必须为 "呵呵" 或 "嘿嘿" 中的一个。');
}
}
}




没有提到的还有客户端验证和独立验证器
https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/input-validation.md#%E7%8B%AC%E7%AB%8B%E9%AA%8C%E8%AF%81%E5%99%A8standalone-validators- https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/input-validation.md#%E5%AE%A2%E6%88%B7%E7%AB%AF%E9%AA%8C%E8%AF%81%E5%99%A8client-side-validation-
参考:https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/input-validation.md

转载请注明出处:http://blog.csdn.net/zhyoulun/article/details/40476805
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  framework yii