Upload File In Renderpartial

Hello,

I’m rendering a partial view in a CJuiTabs’ tab with a form that contain a fileField. The $_POST var has no value for this fileField so I’m unable to upload the file.




<?php $form=$this->beginWidget('CActiveForm', array(

	'id'=>'my-form',

	'enableAjaxValidation'=>true,

        'htmlOptions' => array('enctype' => 'multipart/form-data'),

        'clientOptions' => array('validateOnSubmit' => true)

)); ?>

....

<div class="row">

		<?php echo $form->labelEx($model,'pdf'); ?>

		<?php echo $form->fileField($model,'pdf'); ?>

		<?php echo $form->error($model,'pdf'); ?>

	</div>

....



in my controller when I print_r the $_POST var, the pdf field is empty…

Is there a known issue with file upload and renderPartial ? I use Yii 1.1.13

Just you need to add your action path and your code will be like this :




<?php $form=$this->beginWidget('CActiveForm', array(

        'id'=>'my-form',

 	'action'=> $model->isNewRecord ? array('controller/action') : array(Yii::app()->controller->id . '/update?id=' . $model->id),

        'enableAjaxValidation'=>true,

        'htmlOptions' => array('enctype' => 'multipart/form-data'),

        'clientOptions' => array('validateOnSubmit' => true)

)); ?>

....

<div class="row">

                <?php echo $form->labelEx($model,'pdf'); ?>

                <?php echo $form->fileField($model,'pdf'); ?>

                <?php echo $form->error($model,'pdf'); ?>

        </div>

Thanks for your reply but the form’s action is ok. The $_POST var is correctly passed except for the fileField. That’s why I don’t understand.

I guess there is an issue when using renderPartial to display a form containing a fileField.

Another suggestion ?

Check your model if you add file rule to pdf elment , or add your model code here .

here you are:




public function rules()

	{

		// NOTE: you should only define rules for those attributes that

		// will receive user inputs.

		return array(

			array('commune_id, name', 'required'),

			array('commune_id', 'numerical', 'integerOnly'=>true),

			array('name, address1, address2, contact, pdf', 'length', 'max'=>255),

                        array('phone', 'length', 'max'=>60),

			array('note', 'safe'),

                        array('pdf','file','types' => 'pdf', 'allowEmpty' => true),

			// The following rule is used by search().

			// Please remove those attributes that should not be searched.

			array('id, commune_id, name, address1, address2, note', 'safe', 'on'=>'search'),

		);

	}



I guess the problem in this line :

array(‘name, address1, address2, contact, pdf’, ‘length’, ‘max’=>255),

delete pdf from above array to be

array(‘name, address1, address2, contact’, ‘length’, ‘max’=>255),

I tried but no success. See below the content of the $_POST var :




Array

(

    [commune_id] => 47

    [name] => test name

    [address1] => test address

    [address2] => test address2

    [contact] => test contact

    [phone] => test phone

    [pdf] => 

    [note] => test note a little bit longer

)



Everything is set except the fileField. Weird.

If I use $_FILES instead of $_POST I can see the file infos… but why it doesn’t work like explained here :http://www.yiiframework.com/wiki/2/how-to-upload-a-file-using-a-model/ ??

Hi

Did you check it without CJuiTabs ?

Also, the submit request is a normal http request ? (no ajax request)

Hi,

Yes it works if don’t use CJuiTabs and a partial View, but this is not an option.

It seems to be a bug, I will examine that deeper.

Thanks