【问题标题】:Yii cant validate file inputYii 无法验证文件输入
【发布时间】:2015-07-23 03:45:06
【问题描述】:

我目前正在 PHP Yii 框架中做一个项目。我有一个要求用户上传文件的表单。在注册过程中,用户上传了文件,但是,当用户提交表单时,在文件输入时总是检测到表单是空白的,就像表单上没有附件一样。下面是代码:

模型 - 候选人简历:

return array(           
      array('resume_file','file','types'=>'doc,docx,pdf', 'allowEmpty'=>true, 'safe'=>true, 'on'=>'register'),
); 

模特 - 候选人:

return array(
      array('can_email,name,repeat_can_email, can_password,repeat_can_password','required', 'on'=>'simplereg'),
);

查看:

$form = $this->beginWidget('bootstrap.widgets.TbActiveForm',array(
    'id'=>'candidate-form',
    'enableAjaxValidation'=>true,
    'type'=>'horizontal',
    'htmlOptions' => array(
            'enctype' => 'multipart/form-data',
            'autocomplete'=>'off', //turn off auto complete in FF
    )
)); 

echo $form->textFieldRow($model,'can_email',array('class'=>'span5','maxlength'=>100)); 
echo $form->textFieldRow($model,'repeat_can_email',array('class'=>'span5','maxlength'=>100)); 
echo $form->passwordFieldRow($model,'can_password',array('class'=>'span5','maxlength'=>100)); 
echo $form->passwordFieldRow($model,'repeat_can_password',array('class'=>'span5','maxlength'=>100)); 
echo $form->fileFieldRow($resume,'resume_file', array('id'=>'resume_file'));  

$this->endWidget();

控制器 - 候选人:

public function actionCreate()
{
    $model = new Candidate();
    $model->setScenario('simplereg');
    $resume = new CandidateResume();
    $resume->setScenario('register');   

    // Uncomment the following line if AJAX validation is needed
    //$this->performAjaxValidation($model);

    if(isset($_POST['Candidate'], $_POST['CandidateResume']))
    {
        $_POST['CandidateResume']['resume_file'] = $resume->resume_file;
        $model->attributes = $_POST['Candidate'];
        $resume->attributes = $_POST['CandidateResume'];

        $uploadedFile = CUploadedFile::getInstance($resume,'resume_file');          

        if($resume->validate() && $model->validate()) 
        {
            $model->save();

            if(!empty($uploadedFile))  // check if uploaded file is set or not
            {
                $saved = $uploadedFile->saveAs(Yii::app()->params['RESUME_PATH'].$model->can_id.'_'.$uploadedFile->getName());
                $resume->resume_file = Yii::app()->params['RESUME_DIR'].$model->can_id.'_'.$uploadedFile->getName();
                $resume->resume_send_ip = Yii::app()->request->userHostAddress;
            }
            $resume->save();

        }
    }

    $this->render('create',array('model'=>$model, 'resume'=>$resume));
}

如果我删除控制器上的验证:

if($resume->validate() && $model->validate()) 

可以保存表单数据并将附件正确放置在文件夹中。但是,我需要对表单进行验证。因此,我不能跳过这部分。

有什么我错过的吗?我已经检查了很多次并研究了解决方案。所有都提供了类似的解决方案,因此我无法弄清楚这些事情。谁能帮我?提前谢谢你。

【问题讨论】:

    标签: php forms validation file-upload yii


    【解决方案1】:

    您没有设置resume_file 属性。它来自$_FILES 而不是来自$_POST

    $resume->attributes = $_POST['CandidateResume'];
    $uploadedFile = CUploadedFile::getInstance($resume,'resume_file');   
    $resume->resume_file = $uploadedFile; //add this line
    

    【讨论】:

    • 我应该在哪里添加行?验证之前还是之后?
    • validate()方法前$uploadedFile初始化后
    • 谢谢!有用!!我已经为这个错误苦苦挣扎了很长时间,找不到答案,结果是因为那条线。我能知道为什么我们需要在验证之前完成那个任务吗?我认为它应该能够自行检测。
    • 因为$resume->attributes = $_POST['CandidateResume']; 没有设置resume_file 属性。 转换为超全局数组$_FILES 不在$_GET$_POST 等中
    • 我意识到另一件事是如果我使用表单数据会保存在数据库中:$model->save();我需要把 false 放在括号内。为什么?有区别吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-14
    • 2023-03-11
    • 2015-07-03
    • 2012-09-12
    • 2019-03-28
    • 2011-02-25
    相关资源
    最近更新 更多