File Validation

I am trying to validate a file upload.

All the file information are stored in a single table which is joined to another table which stores the label, the scenario and some other information.

If I upload a file that is not of the require type it still validates as true (I am testing using an .ico file of size about 4.5kB). I have checked using CModel->getValidators() and it is finding the correct validators (safe and file on image).

The model rules are:


public function rules() {

		return array(

			array('id, assetType, data, created', 'safe'),

			array('data', 'file', 'maxSize'=>2097152, 'types'=>'pdf', 'maxFiles' => 6, 'allowEmpty'=>true, 'on'=>'pdf', 'message'=>'Please select a pdf file'),

			array('data', 'file', 'maxSize'=>2097152, 'types'=>'bim', 'maxFiles' => 6, 'allowEmpty'=>true, 'on'=>'bim', 'message'=>'Please select a bim file'),

			array('data', 'file', 'maxSize'=>2097152, 'types'=>'jpg, gif, png', 'maxFiles' => 6, 'allowEmpty'=>true, 'on'=>'image', 'message'=>'Please select either a jpg, gif or png file'),

		);

	}

The controller validation is (I have multiple models on the form):


foreach ($assetType_list as $id => $asset) {

                            if(isset($_POST['Asset'][$id])) {

                                $asset_list[$id]->assetType = $asset->id;

                                $asset_list[$id]->data = CUploadedFile::getInstance($asset_list[$id], "[$id]data");

                                $asset_list[$id]->created = date("Y-m-d H:i:s", time());

				$asset_list[$id]->setScenario($asset->fileType);

                                $valid = $asset_list[$id]->validate() && $valid;

                                if($asset_list[$id]->data->getHasError()){

                                   

                                   $fileError = '';

                                   switch ($asset_list[$id]->data->getError()) {

                                       case 1:

                                            $fileError = 'Please upload a jpg, gif or png file less than 2MB';

                                            break;


                                       default:

                                           break;

                                   }

                                   if($fileError != ''){

                                       $valid = false;

                                       $asset_list[$id]->addError('data', $fileError);

                                   }

                                }

                            }

                        }

What I am missing?

Thank you in advance

Hi Stephen.

Are you sure that your file upload request is not empty?

Try to var_dump($_FILES) array or disable ‘allowEmpty’ option to verify this


Zvook

ZVook

Thank you for you response.

I have var_dump($asset_list[$id]) which storeds the $_FILES info inside $asset_list[$id]->data, and I that is how I would expect it. I will double check $asset_list[$id] and $_FILES, but the problem is with validation not the upload.

Any other ideas?

Stephen