I am having some troubles getting a multi-file upload form working as planned. Here is the relevant code, trimmed down to the essentials.
modules/picture/PictureModule.php
<?php
public $basicNumUploads = 5;
?>
modules/picture/models/Upload.php
<?php
class Upload extends CFormModel
{
public $file;
public function rules()
{
return array(
array('file', 'file',
'maxFiles' => Yii::app()->getModule('picture')->basicNumUploads,
'types' => 'gif png jpg jpeg',
'maxSize' => Yii::app()->getModule('picture')->maxFileSize,
'allowEmpty' => TRUE,
),
);
}
}
?>
modules/picture/controllers/UploadController.php
public function actionIndex()
{
for ($i = 0; $i < Yii::app()->getModule('picture')->basicNumUploads; $i++)
$models[] = new Upload;
if (isset($_POST['Upload']))
{
foreach ($models as $index => $model)
{
$model->attributes = $_POST['Upload'];
$model->file = CUploadedFile::getInstance($model, "[$index]file");
var_dump($model->validate());
}
}
$this->render('index', array('models' => $models));
}
modules/picture/views/upload/index.php
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'htmlOptions' => array('enctype' => 'multipart/form-data'))); ?>
<?php foreach($models as $index => $model): ?>
<?php echo $form->error($model, "[$index]file"); ?>
<?php echo $form->fileField($model, "[$index]file"); ?><br />
<?php endforeach;?>
<?php echo CHtml::submitButton('Upload'); ?>
<?php $this->endWidget(); ?>
</div>
The problem is the following. When I set $basicNumUploads to 1, the form validates correctly, and the var_dump shows FALSE. However, when I set the number to 2 or more, the validation is thrown off. It will always return TRUE, regardless of the fact that I try to upload an invalid file.
Any clues what I might be missing?