我的文件字段与此链接在我的项目中所说的完全一样,我准确地输入了它的代码并更改了所有内容,但是当我希望通过此代码获得它的值时:
$uploadedFile = CUploadedFile::getInstanceByName($model,'image');
图像为空,我收到此错误:
isset 中的非法偏移类型或为空
我看到 Yii 在表单中添加了两个文件字段,第一个是隐藏的和空的,为什么?
我假设 Yii 把它作为主图像,所以它总是空的,我怎么能得到文件字段值。
唯一的区别是它使用 CActiveRecord 但我使用 CFormModel 因为我在这个项目中没有数据库。
我可以通过 $model->fieldname 访问所有字段,但该字段为空!!
编辑:
我找到了这个教程,但我不能使用它,因为我的表单在控制器中,查看文件并从控制器获取很多变量,而且这个教程表单看起来很干净:
$form = new CForm('application.views.fileUpload.uploadForm', $model);
我的观点:
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'contacts-form',
'enableAjaxValidation' => false,
'htmlOptions' => array('enctype' => 'multipart/form-data'),
)); ?>
<div class="fieldtext">
<div class="labelbox">
<?php echo $form->labelEx($model, Yii::t($this->language,'dialogboxname').":"); ?>
</div>
<?php echo $form->textField($model,'name',array('value'=>'aaa','size'=>10,'maxlength'=>50,'class'=>'inputbox')); ?>
</div>
<div class="fieldtext">
<div class="labelbox">
<?php echo $form->labelEx($model,Yii::t($this->language,'dialogboxemail').":"); ?>
</div>
<?php echo $form->textField($model,'email',array('value'=>'aaa@aa.com','size'=>17,'maxlength'=>50,'class'=>'inputbox')); ?>
</div>
<div class="fieldtext subjectbox">
<div class="labelbox">
<?php echo $form->labelEx($model,Yii::t($this->language,'dialogboxsubject').":"); ?>
</div>
<?php echo $form->textField($model,'subject',array('value'=>'aaa','size'=>36,'maxlength'=>50,'class'=>'inputbox')); ?>
</div>
<div class="fieldtext bodybox">
<div class="labelbox">
<?php echo $form->labelEx($model,Yii::t($this->language,'dialogboxbody').":"); ?>
</div>
<?php echo $form->textArea($model,'body',array('value'=>'aaa','rows'=>6, 'cols'=>35,'class'=>'inputbox')); ?>
</div>
<div class="fieldtext bodybox">
<?php echo $form->labelEx($model,'image'); ?>
<?php echo $form->fileField($model, 'image'); ?>
</div>
<?php if(CCaptcha::checkRequirements()): ?>
<div class="captchabox fieldtext">
<div>
<?php echo $form->labelEx($model,Yii::t($this->language,'dialogboxconfirmcode').":"); ?>
<?php $this->widget('CCaptcha',array(
'showRefreshButton' => false,
'clickableImage' => true,
)); ?>
<?php echo $form->textField($model,'verifyCode',array('size'=>5,'maxlength'=>5)); ?>
</div>
</div>
<?php endif; ?>
<div class="contactbuttom">
<div class="submitcontactform buttomsize">
<?php echo CHtml::submitButton(Yii::t($this->language,'dialogboxsendbuttomn')); ?>
</div>
</div>
<?php $this->endWidget(); ?>
我的模型:
<?php
class ContactForm extends CFormModel {
public $name;
public $email;
public $subject;
public $body;
public $image;
public $verifyCode;
/**
* Declares the validation rules.
*/
public function rules()
{
return array(
// name, email, subject and body are required
array('name, email, subject, body', 'required',
),
// email has to be a valid email address
array('email', 'email'),
array('image', 'file'),
}
public function attributeLabels()
{
return array(
'verifyCode'=>'Verification Code',
);
}
}
我的控制器:
...
$this->model= new ContactForm;
$model = $this->model;
if(isset($_POST['ContactForm']))
{
$model->attributes=$_POST['ContactForm'];
if($model->validate())
{
$name = $model->name;
$email = $model->email;
$subject = $model->subject;
$body = $model->body;
$uploadedFile = CUploadedFile::getInstanceByName($model,'image');
....
有趣的是它检查了规则但说它是空的。