2013-02-05 69 views
6

在日誌嗨,我已經警告字符串,同時插入和更新動作無法設置不安全的屬性

2013/02/05 16:43:57 [warning] [application] Failed to set unsafe attribute "logo" of "Model". 

規則模型

public function rules() 
{ 
    return array(
     array('typeId, cityId, new', 'numerical', 'integerOnly'=>true), 
     array('title, url', 'length', 'max'=>255), 
     array('content, created, deleted', 'safe'), 

     array('url', 'url', 'on'=>'insert, update'), 

     array('typeId, cityId, title', 'required', 'on'=>'insert, update'), 

     array('logo', 'file', 'types'=>'jpg, jpeg, gif, png', 'maxSize'=>100*1024, 'allowEmpty'=>true, 'tooLarge'=>'{attribute} is too large to be uploaded. Maximum size is 100kB.'), 

     array('id, typeId, cityId, title, content, new, url, logo', 'safe', 'on'=>'search'), 
    ); 
} 

我不明白爲什麼我得到這個worning。我有標識字段的規則,自己也

回答

16

CFileValidator默認是不安全的,從docs

安全財產(因爲v1.1.12可用)公共布爾$安全;

是否與此驗證器中列出的屬性應考慮 安全的大規模分配。對於這個驗證它默認爲false。

array('logo', 'file', 'types'=>'jpg, jpeg, gif, png','safe'=>true, 'maxSize'=>100*1024, 'allowEmpty'=>true, 'tooLarge'=>'{attribute} is too large to be uploaded. Maximum size is 100kB.'), 
+1

謝謝!!!!!! – dr0zd

3

你必須設置allowEmpty選項safeCFileValidator屬性爲true

array('logo', 'file', 'types'=>'jpg, jpeg, gif, png','safe'=>true, 'maxSize'=>100*1024, 'allowEmpty'=>true, 'tooLarge'=>'{attribute} is too large to be uploaded. Maximum size is 100kB.'), 
+0

謝謝!!!!!! – dr0zd

0

所以設置安全屬性在Yii2

可能的原因,你得到這個錯誤是由於「的形式是enctype」沒有文件上傳設置正確。

Failed to set unsafe attribute 'id' in 

啓用形式的multipart/form-data的

// Form 
$form = ActiveForm::begin(['options'=>['enctype'=>'multipart/form-data']]);