I am try upload a lot files in tabular input, I can upload get and saveAs it, and also can control all others file like textField’s validate, but only the fileField’s validate not working… somebody help. ><
here is the code…
controller
public function actionUpdateAll()
{
for ($i=1;$i<7;$i++)
$PMaps[$i]=$this->loadModel($i);
if(isset($_POST['PPreviewMap']))
{
$valid = true;
foreach ($PMaps as $k=>$PMap)
{
$tmp_img_old = $PMap->img;
$PMap->attributes = $_POST['PPreviewMap']["$k"];
if (@!empty($_FILES['PPreviewMap']['name']["$k"]['img'])){
$PMap->img = Map::model()->generateRandomName(0);
//validate, oldname, newname, waiting to save and saveAs
if ($PMap->validate(array("img"))){ //this not working.....
$tmp_img = CUploadedFile::getInstance($PMap, "[$k]img");
echo '<pre>';
print_r($tmp_img);
echo '</pre>';
$tmp_img->saveAs(Yii::app()->basePath.'/../map_img/'.$PMap->img);
}
}else {
$PMap->img = $tmp_img_old==''? 'default.jpg' : $tmp_img_old;
}
$valid = $valid && $PMap->validate();
}
if ($valid)
echo 'All Pass';
}
$this->render('updateAll',
array('PMaps' =>$PMaps)
);
}
view
<?php echo $this->renderPartial('_formAll', array('PMaps'=>$PMaps)); ?>
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'ppreview-map-form',
'enableAjaxValidation'=>false,
'htmlOptions' =>array('enctype'=>"multipart/form-data" )
)); ?>
<?php
foreach ($PMaps as $PMap)
echo '<p>'.$form->errorSummary($PMap).'</p>';
?>
<?php foreach ($PMaps as $k=>$PMap):?>
<div class="row">
<?php echo $form->labelEx($PMap,"[$k]pre_map_title"); ?>
<?php echo $form->textField($PMap,"[$k]pre_map_title",array('size'=>32,'maxlength'=>32)); ?>
<?php echo $form->error($PMap,$k."[$k]pre_map_title"); ?>
</div>
<div class="row">
<?php echo $form->labelEx($PMap,"[$k]img"); ?>
<?php echo $form->fileField($PMap,"[$k]img",array('size'=>60,'maxlength'=>150)); ?>
<?php echo $form->error($PMap,"[$k]img"); ?>
</div>
<div class="row">
<?php echo $form->labelEx($PMap,"[$k]url"); ?>
<?php echo $form->textField($PMap,"[$k]url",array('size'=>60,'maxlength'=>1500)); ?>
<?php echo $form->error($PMap,"[$k]url"); ?>
</div>
<hr/>
<?php endforeach;?>
model
array('img', 'file', 'allowEmpty'=>true, 'types'=>'jpg','maxSize'=>1024*1024*1, 'tooLarge'=>'Max :1MB'),
I need it work or a way to make everything run. thank you.