First I want to say that I've been using Yii to port my site to PHP from Ruby on Rails and have thus far been very pleased with it.
However, I've come across something that I think may be a bug, though perhaps I'm not setting things up correctly.
I've got file uploading/validating working perfectly with a single file, but with multiple files, both CUploadedFile and the File Validator fail. CUploadedFile doesn't return an object and File Validator fails to see the specific field.
Is it a limitation? Or do I need to do something differently?
Any help would be appreciated.
Thanks!
The following code snippets are condensed by only including pertinent code/markup.
I've got a controller setup like so:
public function actionCreate() { ... $images = array(); $i = 0; while($i < 5) { $images[] = new ReleaseImage; $i++; } ... if(isset($_POST['Release'])) { $release->attributes=$_POST['Release']; if(isset($_POST['submit']) && $release->validate()) { ... //validate images foreach($images as $i => $image) { if(isset($_POST['ReleaseImage'][$i])) { $image->attributes = $_POST['ReleaseImage'][$i]; $image->image = CUploadedFile::getInstance($image, "imageFile[$i]"); } if(($image->sort !== '') || ($image->caption !== '')) { $valid = $valid && $image->validate(); } } ... } } ... }
I've got a form with the following:
<?php echo CHtml::form('', 'post', array('enctype' => 'multipart/form-data')); ?> ... <?php foreach($images as $i => $image): ?> <p> <?php echo CHtml::activeLabel($image,"sort[$i]"); ?> <?php echo CHtml::activeTextField($image,"sort[$i]", array('class' => 'narrow')); ?> <?php echo CHtml::activeLabel($image,"type[$i]", array('class' => 'narrow')); ?> <?php echo CHtml::activeDropDownList($image, "type[$i]", ReleaseImage::types()); ?> <?php echo CHtml::activeLabel($image,"caption[$i]"); ?> <?php echo CHtml::activeTextField($image,"caption[$i]"); ?> </p> <p> <?php echo CHtml::activeLabel($image,"imageFile[$i]"); ?> <?php echo CHtml::activeFileField($image,"imageFile[$i]"); ?> </p> <hr /> <?php endforeach; ?>
In my model I've got the following rule:
public function rules() { return array( ... array('imageFile', 'file'), ); }