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> <?php echo $form->errorSummary($model); ?> <div class="row"> <?php echo $form->labelEx($model,'product_name'); ?> <?php echo $form->textField($model,'product_name',array('size'=>60,'maxlength'=>255)); ?> <?php echo $form->error($model,'product_name'); ?> </div> <div class="row"> <?php echo $form->labelEx($model,'product_short_description'); ?> <?php echo $form->textArea($model,'product_short_description',array('rows'=>6, 'cols'=>50)); ?> <?php echo $form->error($model,'product_short_description'); ?> </div> <div class="row"> <?php echo $form->labelEx($model,'product_long_description'); ?> <?php echo $form->textArea($model,'product_long_description',array('rows'=>6, 'cols'=>50)); ?> <?php echo $form->error($model,'product_long_description'); ?> </div> <div class="row"> <?php echo $form->labelEx($model,'product_avg_price'); ?> <?php echo $form->textField($model,'product_avg_price',array('size'=>10,'maxlength'=>10)); ?> <?php echo $form->error($model,'product_avg_price'); ?> </div> <div class="row"> <?php echo $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--')); ?> <?php echo $form->error($model,'product_brand_id'); ?> </div> <div class="row"> <?php echo $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> <?php echo $form->error($model,'product_category_id'); ?> </div> <div class="row"> <?php echo $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')); ?> <?php echo $form->error($model,'product_main_page_appear_flag'); ?> </div> <div class="row"> <?php echo $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)); ?> <?php echo $form->error($model,'product_avg_rating'); ?> </div> <div class="row"> <?php echo $form->labelEx($model,'product_image_1'); ?> <img src='<?php echo Yii::app()->request->baseUrl; ?>/images/product_images/<?echo $model->product_image_1?>'> <?php echo $form->fileField($model,'product_image_1'); ?> <?php echo $form->error($model,'product_image_1'); ?> </div> <div class="row"> <?php echo $form->labelEx($model,'product_image_2'); ?> <img src='<?php echo Yii::app()->request->baseUrl; ?>/images/product_images/<?echo $model->product_image_2?>'> <?php echo $form->fileField($model,'product_image_2'); ?> <?php echo $form->error($model,'product_image_2'); ?> </div> <div class="row"> <?php echo $form->labelEx($model,'product_image_3'); ?> <img src='<?php echo Yii::app()->request->baseUrl; ?>/images/product_images/<?echo $model->product_image_3?>'> <?php echo $form->fileField($model,'product_image_3'); ?> <?php echo $form->error($model,'product_image_3'); ?> </div> <div class="row"> <?php echo $form->labelEx($model,'product_twitter_text'); ?> <?php echo $form->textField($model,'product_twitter_text',array('size'=>60,'maxlength'=>255)); ?> <?php echo $form->error($model,'product_twitter_text'); ?> </div> <div class="row"> <?php echo $form->labelEx($model,'product_facebook_text'); ?> <?php echo $form->textField($model,'product_facebook_text',array('size'=>60,'maxlength'=>255)); ?> <?php echo $form->error($model,'product_facebook_text'); ?> </div> <div class="row buttons"> <?php echo 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. */ public function actionCreate() { $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 */ public function actionUpdate($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, )); } ?>