In Model file function rules() :
In View file set form like this :
In Controller file edit actionCreate and actionUpdate functions like this:
<?php
array('product_image_1, product_image_2, product_image_3', 'file', 'types'=>'jpg, gif, png', 'allowEmpty'=>true),
?>
In View file set form like this :
<div class="form">
<?php$form=$this->beginWidget('CActiveForm', array(
'id'=>'products-form',
'enableAjaxValidation'=>false,
'htmlOptions'=>array('enctype'=>'multipart/form-data')
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?phpecho$form->errorSummary($model); ?>
<div class="row">
<?phpecho$form->labelEx($model,'product_name'); ?>
<?phpecho$form->textField($model,'product_name',array('size'=>60,'maxlength'=>255)); ?>
<?phpecho$form->error($model,'product_name'); ?>
</div>
<div class="row">
<?phpecho$form->labelEx($model,'product_short_description'); ?>
<?phpecho$form->textArea($model,'product_short_description',array('rows'=>6, 'cols'=>50)); ?>
<?phpecho$form->error($model,'product_short_description'); ?>
</div>
<div class="row">
<?phpecho$form->labelEx($model,'product_long_description'); ?>
<?phpecho$form->textArea($model,'product_long_description',array('rows'=>6, 'cols'=>50)); ?>
<?phpecho$form->error($model,'product_long_description'); ?>
</div>
<div class="row">
<?phpecho$form->labelEx($model,'product_avg_price'); ?>
<?phpecho$form->textField($model,'product_avg_price',array('size'=>10,'maxlength'=>10)); ?>
<?phpecho$form->error($model,'product_avg_price'); ?>
</div>
<div class="row">
<?phpecho$form->labelEx($model,'product_brand_id'); ?>
<?php
//echo $form->textField($model,'product_brand_id');
echo$form->dropDownList($model,'product_brand_id', CHtml::listData(Brands::model()->findAll(), 'brand_id', 'brand_name'), array('empty'=>'--please select--'));
?>
<?phpecho$form->error($model,'product_brand_id'); ?>
</div>
<div class="row">
<?phpecho$form->labelEx($model,'product_category_id'); ?>
<?php
//echo $form->textField($model,'product_category_id');
//echo $form->dropDownList($model,'product_category_id', CHtml::listData(Categories::model()->findAll(), 'category_id', 'category_name'));
?>
<select name='Products[product_category_id]'>
<option value='0'>No Parent</option>
<?php
echo Categories::model()->get_category_hr(0, $model->product_category_id, '');
?>
</select>
<?phpecho$form->error($model,'product_category_id'); ?>
</div>
<div class="row">
<?phpecho$form->labelEx($model,'product_main_page_appear_flag'); ?>
<?php
//echo $form->textField($model,'product_main_page_appear_flag');
echo$form->dropDownList($model,'product_main_page_appear_flag', array('0'=>'No', '1'=>'yes'));
?>
<?phpecho$form->error($model,'product_main_page_appear_flag'); ?>
</div>
<div class="row">
<?phpecho$form->labelEx($model,'product_avg_rating'); ?>
<?php
//echo $form->textField($model,'product_avg_rating');
echo$form->dropDownList($model,'product_avg_rating', array(0,1,2,3,4,5));
?>
<?phpecho$form->error($model,'product_avg_rating'); ?>
</div>
<div class="row">
<?phpecho$form->labelEx($model,'product_image_1'); ?>
<img src='<?phpecho Yii::app()->request->baseUrl; ?>/images/product_images/<?echo$model->product_image_1?>'>
<?phpecho$form->fileField($model,'product_image_1'); ?>
<?phpecho$form->error($model,'product_image_1'); ?>
</div>
<div class="row">
<?phpecho$form->labelEx($model,'product_image_2'); ?>
<img src='<?phpecho Yii::app()->request->baseUrl; ?>/images/product_images/<?echo$model->product_image_2?>'>
<?phpecho$form->fileField($model,'product_image_2'); ?>
<?phpecho$form->error($model,'product_image_2'); ?>
</div>
<div class="row">
<?phpecho$form->labelEx($model,'product_image_3'); ?>
<img src='<?phpecho Yii::app()->request->baseUrl; ?>/images/product_images/<?echo$model->product_image_3?>'>
<?phpecho$form->fileField($model,'product_image_3'); ?>
<?phpecho$form->error($model,'product_image_3'); ?>
</div>
<div class="row">
<?phpecho$form->labelEx($model,'product_twitter_text'); ?>
<?phpecho$form->textField($model,'product_twitter_text',array('size'=>60,'maxlength'=>255)); ?>
<?phpecho$form->error($model,'product_twitter_text'); ?>
</div>
<div class="row">
<?phpecho$form->labelEx($model,'product_facebook_text'); ?>
<?phpecho$form->textField($model,'product_facebook_text',array('size'=>60,'maxlength'=>255)); ?>
<?phpecho$form->error($model,'product_facebook_text'); ?>
</div>
<div class="row buttons">
<?phpecho CHtml::submitButton($model->isNewRecord?'Create':'Save'); ?>
</div>
<?php$this->endWidget(); ?>
</div><!-- form -->
In Controller file edit actionCreate and actionUpdate functions like this:
<?php
/**
* Creates a new model.
* If creation is successful, the browser will be redirected to the 'view' page.
*/
publicfunctionactionCreate() {
$model=new Products;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if (isset($_POST['Products'])) {
$model->attributes=$_POST['Products'];
$model->product_image_1= CUploadedFile::getInstance($model, 'product_image_1');
$model->product_image_2= CUploadedFile::getInstance($model, 'product_image_2');
$model->product_image_3= CUploadedFile::getInstance($model, 'product_image_3');
if ($model->save()) {
$model->product_image_1->saveAs('images/product_images/'.$model->product_image_1);
$model->product_image_2->saveAs('images/product_images/'.$model->product_image_2);
$model->product_image_3->saveAs('images/product_images/'.$model->product_image_3);
$this->redirect(array('view', 'id'=>$model->product_id));
// redirect to success page
}
//if($model->save())
// $this->redirect(array('view','id'=>$model->product_id));
}
$this->render('create', array(
'model'=>$model,
));
}
/**
* Updates a particular model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param integer $id the ID of the model to be updated
*/
publicfunctionactionUpdate($id) {
$model=$this->loadModel($id);
$model_old_data=$this->loadModel($id);
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if (isset($_POST['Products'])) {
$model->attributes=$_POST['Products'];
if ($model->product_image_1= CUploadedFile::getInstance($model, 'product_image_1')) {
$model->product_image_1->saveAs('images/product_images/'.$model->product_image_1);
$attributes['product_image_1'] =$model->product_image_1;
} else {
$model->product_image_1=$model_old_data->product_image_1;
}
if ($model->product_image_2= CUploadedFile::getInstance($model, 'product_image_2')) {
$model->product_image_2->saveAs('images/product_images/'.$model->product_image_2);
$attributes['product_image_2'] =$model->product_image_2;
} else {
$model->product_image_2=$model_old_data->product_image_2;
}
if ($model->product_image_3= CUploadedFile::getInstance($model, 'product_image_3')) {
$model->product_image_3->saveAs('images/product_images/'.$model->product_image_3);
$attributes['product_image_3'] =$model->product_image_3;
} else {
$model->product_image_3=$model_old_data->product_image_3;
}
if ($model->save()) {
$this->redirect(array('view', 'id'=>$model->product_id));
// redirect to success page
}
}
$this->render('update', array(
'model'=>$model,
));
}
?>