Pergunta

I need to upload file with Yii. In view I have row: <?php echo CHtml::activefileField($qualificationModel, 'picture'); ?>, where

$qualificationModel = new SkillsMapping;

Part of controller:

 $skillsModel = SkillsMapping::model();
 $skillsModel->attributes=$_POST['SkillsMapping'];
 $skillsModel->picture=CUploadedFile::getInstance($skillsModel,'picture');
 echo  var_dump($_FILES);
 if($skillsModel->validate())
 {
    if($skillsModel->save())
    {
       $skillsModel->picture->saveAs('images/qual-pics');
       $this->redirect(array('view','id'=>$model->user_id));
    }
 }

When i chose photo and clicked button i have a error Picture cannot be blank. After checking POST request i founded strange thing - request have two attributes - SkillsMapping[picture]= and SkillsMapping[picture]=Lighthouse.jpg. If commented echo in top of this post, both attributes disappear. How to remove empty SkillsMapping[picture] and why it's going? Thanks.

Foi útil?

Solução

You do not need to remove the empty hidden field because it's there to help you, not hurt you (Yii itself automatically puts it there; see the relevant part of the source).

The hidden field is there so that $_POST is populated with an empty value when no file has been selected to upload. If a file has been selected the file input control will provide its own POST value that overwrites the empty "guard value".

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top