Hi我在插入和更新操作时在日志中有警告字符串
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'),
);
}
我不明白为什么我会这么担心。我有徽标字段的规则,并为其提供allowEmpty选项
CFileValidator默认不安全,来自文档:
safe属性(自v11.12起可用)公共布尔值$safe;
是否应该考虑与该验证器一起列出的属性对于大规模任务来说是安全的。对于这个验证器,它默认为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.'),
您必须将CFileValidator
的safe
属性设置为真正的
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.'),
在Yii2中
出现此错误的可能原因是没有为文件上载正确设置表单"enctype"。
Failed to set unsafe attribute 'id' in
启用表单多部分/表单数据
// Form
$form = ActiveForm::begin(['options'=>['enctype'=>'multipart/form-data']]);