File upload with Yii's ActiveForm

2019-07-23 13:16发布

问题:

I am trying to use Yii's ActiveForm to create a basic registration page with an image upload field. However, I am running into problems. I am using the following code to create the form tags:

$form=$this->beginWidget('CActiveForm', array(
                                'id'=>'activity_form', 
                                'enableAjaxValidation'=>true, 
                                'stateful'=>true, 
                                'enctype'=>'multipart/form-data'
                                ));

The above code produces the following error message in Yii:

Property "CActiveForm.enctype" is not defined

I've also tried:

$form=$this->beginWidget('CActiveForm', array(
                                'id'=>'activity_form', 
                                'enableAjaxValidation'=>true, 
                                'stateful'=>true, 
                                array('enctype'=>'multipart/form-data')));

as well as:

$form=$this->beginWidget('CActiveForm', array(
                                'id'=>'activity_form', 
                                'enableAjaxValidation'=>true, 
                                'stateful'=>true), 
                                array('enctype'=>'multipart/form-data')));

But neither of these work.

Any ideas as to what could be wrong? Can I use beginWidget to create a multipart form with file upload capabilities? What's the format I should follow for this? I can't seem to find any answers in the documentation or the forums.

Thanks!

回答1:

Never mind. I found the solution to this. The trick is to use htmlOptions like so:

$this->beginWidget('CActiveForm', array(
  'id'=>'activity_form', 
  'enableAjaxValidation'=>true, 
  'stateful'=>true, 
  'htmlOptions'=>array('enctype' => 'multipart/form-data')
));