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'),
    );
}

我不明白为什么我会穿这个。我有 logo 字段的规则,并且有 allowEmpty 选项

4

3 回答 3

16

CFileValidator默认情况下是不安全的,来自文档

安全属性(自 v1.1.12 起可用) public boolean $safe;

与此验证器一起列出的属性是否应该被认为对于大规模分配是安全的。对于此验证器,它默认为 false。

所以将 safe 属性设置为 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.'),
于 2013-02-05T14:02:49.593 回答
3

您必须将safe属性设置CFileValidator为 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.'),
于 2013-02-05T14:01:30.730 回答
0

在 Yii2

由于没有为文件上传正确设置表单“enctype”,可能导致您收到此错误。

Failed to set unsafe attribute 'id' in 

启用表单 multipart/form-data

// Form
$form = ActiveForm::begin(['options'=>['enctype'=>'multipart/form-data']]);
于 2015-07-18T16:30:17.773 回答