Reputation: 181
I have file field exactly as this link said in my project , i put exactly it's code and change every thing looks like it but when i want it's value by this code :
$uploadedFile = CUploadedFile::getInstanceByName($model,'image');
image is empty and i get this error :
Illegal offset type in isset or empty
I saw Yii add two field for file in the form and first one is hidden and empty why ?
And i assume Yii get it as main image and so it is always empty , How could i get file field value.
The only difference is it use CActiveRecord but i use CFormModel beacuse i dont have database in this project.
I can access all the fields by $model->fieldname but this field is empty !!
Edited:
I find this tutorial but i cant use it because my form is in the controllers, view file and get a lot of variable from controller and this tutorials form seems clean:
$form = new CForm('application.views.fileUpload.uploadForm', $model);
My view:
<?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'=>'[email protected]','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(); ?>
My Model:
<?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',
);
}
}
My Controller:
...
$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');
....
Funny thing is it check the rule but said it is empty.
Upvotes: 3
Views: 9520
Reputation: 685
This is my answer:
public function actionCreate()
{
$model = new Answer();
var_dump(Yii::$app->request->post());`enter code here`
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
$model->answer_by= \Yii::$app->user->getId();
$name = $model->answer_attachment;
var_dump($name);
$model->answer_attachment = UploadedFile::getInstanceByName('btn.jpg');
var_dump($model->answer_attachment);
$time=time();
if(!empty($model->answer_attachment)){
$model->answer_attachment->saveAs('images/answer/'.$time.'.'.$model->answer_attachment->extension);
$model->answer_attachment='images/answer/'.$time.'.'.$model->answer_attachment->extension;
}
$model->answer_timestamp=$time;
//$model->save();
//return $this->redirect(['view', 'id' => $model->answer_id]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
Upvotes: 0
Reputation: 4461
Your code:
$uploadedFile = CUploadedFile::getInstanceByName($model,'image');
has to be replaced with this:
$uploadedFile = CUploadedFile::getInstance($model,'image');
CUploadedFile::getInstanceByName
takes one paramerter, not two https://github.com/yiisoft/yii/blob/master/framework/web/CUploadedFile.php#L81
It requires name of file-input. So, call it like this:
CUploadedFile::getInstanceByName('ContactForm[image]'); //if 'ContactForm[image]' is input name
Actually, CUploadedFile::getInstance
calles it as well. But it also resolves input name itself https://github.com/yiisoft/yii/blob/master/framework/web/CUploadedFile.php#L57
So, I believe, CUploadedFile::getInstance
is better approach.
PS: Not sure why you use CUploadedFile::getInstanceByName
even though tutorial you refered to uses CUploadedFile::getInstance
in controller.
Be attentive :)
Upvotes: 1
Reputation: 25312
The following line is wrong :
$uploadedFile = CUploadedFile::getInstanceByName($model,'image');
http://www.yiiframework.com/doc/api/1.1/CUploadedFile#getInstanceByName-detail
You should try :
$uploadedFile = CUploadedFile::getInstanceByName('ContactForm[image]');
Or:
$uploadedFile = CUploadedFile::getInstance($model,'image');
Upvotes: 1