2013-02-05 68 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']]);