用Yii的ActiveForm上传文件 [英] File upload with Yii's ActiveForm

查看:374
本文介绍了用Yii的ActiveForm上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用Yii的ActiveForm创建一个带有图片上传字段的基本注册页面。但是,我遇到了问题。我使用下面的代码来创建表单标记:

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

以上代码在Yii中产生以下错误消息:

属性CActiveForm.enctype未定义



我也试过:

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

以及:

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

但这两者都不起作用。

任何想法可能是错误的?我可以使用beginWidget创建具有文件上传功能的多部分表单吗?我应该遵循什么格式?我似乎无法在文档或论坛中找到任何答案。



谢谢!

解决方案

没关系。我找到了解决办法。诀窍是像这样使用htmlOptions:

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


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!

解决方案

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')
));

这篇关于用Yii的ActiveForm上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆