搜尋
首頁php教程php手册yii ar中的rules()规则在收集表单中的一些作用

当使用Massive Assignment时,如果form内的数据没有在rules验证规则中明确的指定,对这个字段的赋值将不会生效, 对于那些没有特殊要求的字段至少也好设置safe规则。 ?phppublic function actionCreate(){$model=new Post;if(isset($_POST['Post'])){$model-

当使用Massive Assignment时,如果form内的数据没有在rules验证规则中明确的指定,对这个字段的赋值将不会生效,

对于那些没有特殊要求的字段至少也好设置safe规则。

<?php public function actionCreate()
{
$model=new Post;
if(isset($_POST['Post']))
{
$model->attributes=$_POST['Post'];//tag A
if($model->save())
$this->redirect(array('view','id'=>$model->id));
}
$this->render('create',array(
'model'=>$model,
));
}

看tag A部分,它执行了一下的流程:

The key is $model->attributes = $_POST['Comment'];, which is deceptively simple.In reality this is calling

$model->setAttributes( $_POST['Comment'] ); // this is an array!

CModel的setAttributes详细代码如下:

<?php public function setAttributes($values,$safeOnly=true)
{
if(!is_array($values))
return;
$attributes=array_flip($safeOnly ? $this->getSafeAttributeNames() : $this->attributeNames());// tag B
foreach($values as $name=>$value)?//tag E
{
if(isset($attributes[$name]))
$this->$name=$value;//tag F
else if($safeOnly)//tag G
$this->onUnsafeAttribute($name,$value);
}
}

看tag B 部分,因为$safeOnly默认是true,所以这里首先开始执行$this->getSafeAttributeNames();

CModel的getSafeAttributeNames详细代码如下:

<?php public function getSafeAttributeNames()
{
$attributes=array();
$unsafe=array();
foreach($this->getValidators() as $validator)?//tag C
{
if(!$validator->safe)
{
foreach($validator->attributes as $name)
$unsafe[]=$name;
}
else
{
foreach($validator->attributes as $name)
$attributes[$name]=true;
}
}
foreach($unsafe as $name)
unset($attributes[$name]);
return array_keys($attributes);
}

看tag C部分,此处开始执行$this->getValidators();

CModel的getValidators详细代码如下:

<?php public function getValidators($attribute=null)
{
if($this->_validators===null)
$this->_validators=$this->createValidators();//tag D
$validators=array();
$scenario=$this->getScenario();
foreach($this->_validators as $validator)
{
if($validator->applyTo($scenario))
{
if($attribute===null || in_array($attribute,$validator->attributes,true))
$validators[]=$validator;
}
}
return $validators;
}

看tag D部分,代码如下:

<?php public function createValidators()
{
$validators=new CList;
foreach($this->rules() as $rule)
{
if(isset($rule[0],$rule[1])) // attributes, validator name
$validators->add(CValidator::createValidator($rule[1],$this,$rule[0],array_slice($rule,2)));
else
throw new CException(Yii::t('yii','{class} has an invalid validation rule. The rule must specify attributes to be validated and the validator name.',
array('{class}'=>get_class($this))));
}
return $validators;
}

可以看到在这个方法中开始使用验证规则,如果$this->rules()是个空数组则foreach内的消息体都不会被执行,直接执行下面的return $validators;

最终返回了空的CList的实例。现在回到getValidators()方法,$this->_validators就是一个空的CList实例,$validators初始化为空数组,getValidators直接返回$validators到getSafeAttributeNames的tag C处,同理getSafeAttributeNames也返回了空的数组给setAttributes的tag B处。

最终$attributes为空数组。在执行tag E处循环时一直执行tag G而不是执行tag F部分,而tag F部分才是真正的绑定值。

参考地址:http://www.yiiframework.com/wiki/161/understanding-safe-validation-rules/

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
4 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強大的PHP整合開發環境

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能