1

我对 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();?>

我希望有人能提供帮助,因为我看了一遍,似乎找不到任何问题。

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

4

1 回答 1

0

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

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

于 2013-07-27T15:16:19.153 回答