我对yii有点新,我遇到了文件上传的问题。 当我上传一个文件它不验证 - 它允许所有类型(它不应该),允许空(它不应该)等 当然,我包括适当的规则(我认为)在我的模型。 它看起来像fileField由于某种原因没有验证。Yii文件上传 - filefield未验证
我想要它写错误消息(错误类型或空),当我点击提交按钮。 如果你找不到任何错误,请看看它。
这里是我的形式:
<?php class UploadSolutionForm extends CFormModel
{
public $sourceCode;
public function rules()
{
return array(
array('sourceCode', 'file', 'types'=>'java, zip', 'allowEmpty'=>false, 'wrongType'=>'Only .java and .zip files.'),
);
}
/**
* Declares attribute labels.
*/
public function attributeLabels()
{
return array(
'sourceCode' => 'Source code',
);
}}
这是我在控制器操作:
public function actionSolveProblem()
{
//gets id from url
$id = Yii::app()->request->getQuery('id');
//guests can't work with problems in any way
if (Yii::app()->user->isGuest)
{
$this->redirect(array('site/index'));
return;
}
if(isset($id))
{
$model = new UploadSolutionForm();
if(isset($_POST['UploadSolutionForm']))
{
$model->attributes=$_POST['UploadSolutionForm'];
$newFile = CUploadedFile::getInstance($model, 'sourceCode');
$problem = Problems::model()->findByAttributes(array('id'=>$id));
$version = Solutions::model()->countByAttributes(array('id_problem'=>$id))+1;
$pathUser = Yii::app()->basePath.'\\SubjectFolder\\'.$problem->id_subject.'\\'.$id.'\\'.Yii::app()->user->getName();
$path = $pathUser.'\\'.$version;
if (!is_dir($pathUser)) {
mkdir($pathUser);
}
if (!is_dir($path)) {
mkdir($path);
}
$uploadPath = $path.'\\'.$newFile->name;
$solution = new Solutions();
$solution->id_problem = $id;
$solution->id_user = Yii::app()->user->getId();
$solution->submit_time = date("Y-m-d H:i:s",time());
$solution->path_to_file = $uploadPath;
$solution->version = $version;
if($newFile->saveAs($uploadPath))
{
if($solution->save())
{
Yii::app()->user->setFlash('new_problem','Solution uploaded');
$this->redirect(array('site/problems'));
}
else
{
Yii::app()->user->setFlash('new_problem','Error! Could not save into db');
$this->redirect(array('site/problems'));
}
}
}
//part bellow is not really important in this case
$auth = Problems::model()->with(array('subject','subject.currentUserSubject' => array('alias'=>'currentUserSubject')))->findByAttributes(array('id'=>$id));
// user with proper role cannot upload solutions to problems
if($auth!=null)
{
$this->render('solve',array('model'=>$model));
}
else
{
Yii::app()->user->setFlash('new_problem','You are not authorized to upload a solution for this problem');
$this->redirect(array('site/problems'));
}
}
}
,这里是我的看法(solve.php):
<?php
$form = $this->beginWidget(
'CActiveForm',
array(
'id' => 'upload-form',
'htmlOptions' => array('enctype' => 'multipart/form-data'),
'enableClientValidation'=>true,
'clientOptions'=>array(
'validateOnSubmit'=>true,
),));
?>
<div class="row">
<?php echo $form->labelEx($model, 'sourceCode');?>
<br><?php echo $form->fileField($model, 'sourceCode');?>
<?php echo $form->error($model, 'sourceCode');?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton('Submit'); ?>
</div>
<?php
// ...
$this->endWidget();?>
希望sombody可以提供帮助,因为我看了很久,我似乎无法找到任何错误。
编辑:我主要是想弄清楚为什么客户端验证未对文件上传工作正常(它为文本字段),当然修复它,以及...
我有同样的问题。这是我的问题的链接,以供我参考,以便我发现问题。 http://stackoverflow.com/questions/17800617/yii-validation-with-file-upload-failing(对不起,这不是问题的答案) – Jonnny
你的控制器代码看起来很奇怪。与其专注于1个模型并进行适当的验证,你会混淆3种不同的模型。所以你错过了几个重要的步骤:你不要把'$ newFile'分配给'$ model-> sourceFile',你似乎不会调用'$ model-> validate()'。看看[this](http://www.yiiframework.com/wiki/2/how-to-upload-a-file-using-a-model)。总而言之:控制器中的模型逻辑太多。您可以将很多代码移入适当的模型中。 –