I created a file uploaded validation rule without a table in my database, so what I did is I just extended it using CFormModel.
Here is my code:
Controller
public function actionMaterials($pid)
{
$projectMaterialFile = new ProjectMaterialFile;
$this->render('project_materials',array(
'projectMaterialFile'=>$projectMaterialFile,
));
}
VIEW (Project Material)
<div id="exportMaterialContent">
<h4 style="text-align:left;">Export Material Document</h4>
<?php echo CHtml::form($this->createUrl("project/export"),'post',array('enctype'=>'multipart/form-data')); ?>
<?php echo CHtml::activeHiddenField( $projectMaterialFile,'idProject',array('value'=>$projectModel->idProject) ); ?>
<?php echo CHtml::activeFileField($projectMaterialFile, 'document'); ?>
<?php echo CHtml::submitButton("Export"); ?>
<?php echo CHtml::endForm(); ?>
</div>
Controller - Export(After the form from the view has been submitted)
public function actionExport()
{
$model = new ProjectMaterialFile;
$model->attributes = $_POST['ProjectMaterialFile'];
if( $model->validate() ) {
$model->document = CUploadedFile::getInstance($model,'document');
$model->document->saveAs(Yii::getPathOfAlias('webroot')."/material_document/".$model->document->name);
echo "correct";
} else {
echo "Invalid!";
}
}
Model
class ProjectMaterialFile extends CFormModel
{
public $document;
public $idProject;
public function rules()
{
return array(
array('document','file','types'=>'csv'),
array('document', 'required'),
);
}
public function attributeLabels()
{
return array(
'document' => 'Project Material Document',
'idProject' => 'Project ID',
);
}
}
So what I did here is that it always run to the else of my controller actionExport()
Even if I try to echo $model->validate();
, it does not print anything that's why it always go to the else statement.
Your help would be greatly appreciated and of course, rewarded!
Thanks! :)