Hi, I’m new to Yii framework. I need help about tabular input. I don’t understand why if I upload files using tabular input, the files are not saved in the folder and the file validator method is not called.
public function actionCreate()
{
$model=new Image;
$models = array();
for($i=0;$i<=1;$i++){
$models[] = new Image;
}
$model = new Image;
if(isset($_POST['Image'])){
foreach($models as $i=>$model){
if(isset($_POST['Image'][$i])){
$model = new Image;
$model->attributes=$_POST['Image'][$i];
$rnd = rand(0,9999);
$uploadedFile=CUploadedFile::getInstance($model,"[$i]image");
$fileName = "{$rnd}-{$uploadedFile}"; // random number + file name
$model->image = $fileName;
if($model->save()){
$uploadedFile->saveAs(Yii::app()->basePath.'/'.$fileName);
$this->redirect(array('view','id'=>$model->id));
}
}
}
}
<?php
/**
* This is the model class for table "tbl_image".
*
* The followings are the available columns in table 'tbl_image':
* @property integer $id
* @property string $name
* @property string $ext
*/
class Image extends CActiveRecord
{
public $image;
/**
* Returns the static model of the specified AR class.
* @param string $className active record class name.
* @return Image the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
/**
* @return string the associated database table name
*/
public function tableName()
{
return 'tbl_image';
}
/**
* @return array validation rules for model attributes.
*/
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
//array('name', 'required'),
array('name, ext', 'length', 'max'=>50),
array('image', 'file', 'types'=>'jpg, gif, png'),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('id, name, ext', 'safe', 'on'=>'search'),
);
}
/**
* @return array relational rules.
*/
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'id' => 'ID',
'name' => 'Name',
'ext' => 'Ext',
);
}
/**
* Retrieves a list of models based on the current search/filter conditions.
* @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
*/
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id);
$criteria->compare('name',$this->name,true);
$criteria->compare('ext',$this->ext,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
}
<?php $form=$this->beginWidget('bootstrap.widgets.TbActiveForm',array(
'id'=>'image-form',
'enableAjaxValidation'=>false,
'htmlOptions' => array('enctype' => 'multipart/form-data'),
)); ?>
<p class="help-block">Fields with <span class="required">*</span> are required.</p>
<?php foreach($models as $i=>$model): ?>
<?php echo $form->errorSummary($model); ?>
<?php echo $form->textFieldRow($model,"[$i]name",array('class'=>'span5','maxlength'=>50)); ?>
<?php echo $form->textFieldRow($model,"[$i]ext",array('class'=>'span5','maxlength'=>50)); ?>
<?php
//echo $form->labelEx($model, "[$i]image");
echo $form->fileField($model, "[$i]image");
//echo $form->error($model, "[$i]image");
?>
<?php endforeach; ?>
<div class="form-actions">
<?php $this->widget('bootstrap.widgets.TbButton', array(
'buttonType'=>'submit',
'type'=>'primary',
'label'=>$model->isNewRecord ? 'Create' : 'Save',
)); ?>
</div>
<?php $this->endWidget(); ?>
Can someone help me?
Thanks