我在插入和更新操作时在日志中有警告字符串
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'),
);
}
我无法理解为什么我会这样做。我有徽标字段的规则,并有forEmpty选项
答案 0 :(得分:16)
安全属性(自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.'),
答案 1 :(得分: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.'),
答案 2 :(得分:0)
在Yii2
由于表单'enctype'未正确设置文件上传,可能会导致此错误。
Failed to set unsafe attribute 'id' in
启用表单multipart / form-data
// Form
$form = ActiveForm::begin(['options'=>['enctype'=>'multipart/form-data']]);