上传文件,验证出错,不能为空? [ 新手入门 ]
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('title,content,cat_name','required'),
array('content', 'safe'),
array('url','file','maxSize'=>800*1024,'tooLarge'=>'文件太大超过800KB',),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('id, title, content, url, cat_name', 'safe', 'on'=>'search'),
);
}
控制器代码
public function actionCreate()
{
$model=new Downloads;
// Uncomment the following line if AJAX validation is needed
$this->performAjaxValidation($model);
if(isset($_POST['Downloads']))
{
$model->attributes=$_POST['Downloads'];
if ($model -> validate()) {//执行验证
{
$model->url=CUploadedFile :: getInstance($model, 'url');//准备上传文件
$extName = strtolower($model ->url->extensionName); //取类型
$newName = date('YmdHis') . '_' . rand(1, 9) . '.' . $extName; //按时间生成新文件名
$path = yii :: app() -> basePath . '/../uploads/downs/' . $newName; //设定上传目录
$model -> url -> saveAs($path); //保存文件
$model -> url=$newName;//重新设好文件名
if($model->save())
$this->redirect(array('view','id'=>$model->id));
}
}
}
$this->render('create',array(
'model'=>$model,
));
}
现在明明选择了文件,也可以正常提交表单,同时也上传了文件,就是提交前显示文件不能为空有点不爽
webyjh
注册时间:2011-12-12
最后登录:2016-10-06
在线时长:1小时45分
最后登录:2016-10-06
在线时长:1小时45分
- 粉丝2
- 金钱1090
- 威望0
- 积分1100