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