duankezong4064 2012-09-28 16:27
浏览 41
已采纳

Yii验证规则不起作用

I created a file uploaded validation rule without a table in my database, so what I did is I just extended it using CFormModel.

Here is my code:

Controller

public function actionMaterials($pid)
{
    $projectMaterialFile = new ProjectMaterialFile;
    $this->render('project_materials',array(
        'projectMaterialFile'=>$projectMaterialFile,
    ));
}

VIEW (Project Material)

<div id="exportMaterialContent">
    <h4 style="text-align:left;">Export Material Document</h4>
    <?php echo CHtml::form($this->createUrl("project/export"),'post',array('enctype'=>'multipart/form-data')); ?>
    <?php echo CHtml::activeHiddenField( $projectMaterialFile,'idProject',array('value'=>$projectModel->idProject) ); ?>
    <?php echo CHtml::activeFileField($projectMaterialFile, 'document'); ?>
    <?php echo CHtml::submitButton("Export"); ?>
    <?php echo CHtml::endForm(); ?>
</div>

Controller - Export(After the form from the view has been submitted)

public function actionExport()
{
    $model = new ProjectMaterialFile;
    $model->attributes = $_POST['ProjectMaterialFile'];
    if( $model->validate() ) {
        $model->document = CUploadedFile::getInstance($model,'document');
        $model->document->saveAs(Yii::getPathOfAlias('webroot')."/material_document/".$model->document->name);
        echo "correct";
    } else {
        echo "Invalid!";
    }
}

Model

class ProjectMaterialFile extends CFormModel
{
    public $document;
    public $idProject;

    public function rules()
    {
        return array(
            array('document','file','types'=>'csv'),
            array('document', 'required'),
        );
    }

    public function attributeLabels()
    {
        return array(
            'document' => 'Project Material Document',
            'idProject' => 'Project ID',
        );
    }
}

So what I did here is that it always run to the else of my controller actionExport() Even if I try to echo $model->validate();, it does not print anything that's why it always go to the else statement.

Your help would be greatly appreciated and of course, rewarded!

Thanks! :)

  • 写回答

1条回答 默认 最新

  • doukengzi3517 2012-09-28 16:43
    关注

    Try to change validation rules in your model as below

    public function rules()
    {
       return array(
          array('document','file','types'=>'csv'),
          array('document', 'required'),
          array('document', 'length', 'max'=>200),
          array('idProject', 'length', 'max'=>200),
          array('document, idProject', 'safe'),
          );
    }
    

    Now change your controller part as below

    $model = new ProjectMaterialFile;
    $model->attributes = $_POST['ProjectMaterialFile'];
    $model->document = CUploadedFile::getInstance($model,'document');
    
    if( $model->validate() )
    {
       $model->document->saveAs(Yii::getPathOfAlias('webroot')
                    ."/material_document/".$model->document->name);
        echo "Correct";
    }
    else
    {
        echo "Invalid!";
    }
    

    Example Link: http://www.yiiframework.com/wiki/2/how-to-upload-a-file-using-a-model/

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机