Yii文件上传 - 未验证文件字段

时间:2013-07-26 18:03:32

标签: file validation upload yii

我是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可以提供帮助,因为我看了一遍,我似乎无法找到任何错误。

编辑:我主要想弄清楚为什么客户端验证无法正常上传文件(适用于文本字段),当然还要修复它...

1 个答案:

答案 0 :(得分:0)

就像迈克尔在评论中所说,您的主要问题是您没有专门将上传文件分配给模型中的文件属性。 Yii文件验证默认设置为不安全(因此,非质量可分配)。

因此,您的上传文件永远不会被您的验证规则处理。