In a form based on CActiveRecord model, I am trying to upload a file, save it in a server folder and save the user specified filename in the database record. I used CHtml::activeFileField($model,‘image’) in the form to select the file to be uploaded. When I run the form, it lets me choose the file and displays its filename on the form. However, when the form is submitted, the validation fails with the message “image cannot be blank” and “No file Chosen” against the “Choose File” button. For some strange reason, it displays the error message, “image cannot be blank” twice! I shall appreciate if someone can tell me what’s going wrong and why I am unable to upload the file. Here are my files:
Model:
<?php
class Products extends CActiveRecord
{
public $image;
public static function model($className=__CLASS__)
{
return parent::model($className);
}
/**
* @return string the associated database table name
*/
public function tableName()
{
return 'tbl_products';
}
/**
* @return array validation rules for model attributes.
*/
public function rules()
{
return array(
array('image', 'file', 'allowEmpty'=>false),
array('image', 'file', 'types'=>'jpg,jpeg,gif,png', 'maxSize'=>30720),
array('productcategory_id, product_name, price,imagefilename','required'),
array('productcategory_id', 'numerical', 'integerOnly'=>true),
array('product_name', 'length', 'max'=>40),
array('price', 'length', 'max'=>10),
array('id, productcategory_id, product_name, price', 'safe', 'on'=>'search'),
);
}
Controller:
/**
* Creates a new model.
* If creation is successful, the browser will be redirected to the 'view' page.
*/
public function actionCreate()
{
$model=new Products;
//$this->performAjaxValidation($model);
if(isset($_POST['Products'])) {
$model->attributes=$_POST['Products'];
if ($model->validate()) {
$uploadedImage = CUploadedFile::getInstance($model,'image');
if ( ($uploadedImage->hasError) ) {
$uploaderror = " Upload error Code: " .$uploadedImage->error ;
$model->imagefilename = $model->imagefilename . $uploaderror ;
} else {
$uploaderror = " No Upload error " ;
// Save Image file in a server folder
$model->imagefilename = $model->imagefilename . $uploadedImage->getName();
//$uploadedImage->saveAs( Yii::app()->request->baseUrl . '\\images\\'. $model->imagefilename);
}
if($model->save()) {
$this->redirect(array('view','id'=>$model->id));
}
}
}
$this->render('create',array(
'model'=>$model,
));
}
View File
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'products-form',
'enableAjaxValidation'=>false
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary($model); ?>
<?php echo CHtml::form('','post',array('enctype'=>'multipart/form-data')); ?>
....
<BR>
<?php echo CHtml::activeLabel($model,'Choose Product Image'); ?>
<?php echo CHtml::activeFileField($model,'image'); ?>
<BR>
<div class="row">
<?php echo $form->labelEx($model,'imagefilename'); ?>
<?php echo $form->textField($model,'imagefilename',array('size'=>50,'maxlength'=>50)); ?>
<?php echo $form->error($model,'imagefilename'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->