Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8595589
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:28:17+00:00 2026-06-12T00:28:17+00:00

I created a file uploaded validation rule without a table in my database, so

  • 0

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! 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T00:28:19+00:00Added an answer on June 12, 2026 at 12:28 am

    Try to change validation rules in your model as below

    public function rules()
    {
       return array(
          array('document','file','types'=>'csv'),
          array('document', 'required'),
          array('document', 'length', 'max'=>200),
          array('idProject', 'length', 'max'=>200),
          array('document, idProject', 'safe'),
          );
    }
    

    Now change your controller part as below

    $model = new ProjectMaterialFile;
    $model->attributes = $_POST['ProjectMaterialFile'];
    $model->document = CUploadedFile::getInstance($model,'document');
    
    if( $model->validate() )
    {
       $model->document->saveAs(Yii::getPathOfAlias('webroot')
                    ."/material_document/".$model->document->name);
        echo "Correct";
    }
    else
    {
        echo "Invalid!";
    }
    

    Example Link: http://www.yiiframework.com/wiki/2/how-to-upload-a-file-using-a-model/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I created a file name called %20%20.txt and uploaded in my webs space. When
I've a custom form (created with form API) that need send an uploaded file
I have created a file named MyFile.db using SQLite3 from my C#.net application. This
I created uploads folder on the server, and a file uploaded by a user
I've created an ajax file uploader for Django, but each file uploaded takes up
I am saving user's uploaded file on the disk and just storing filename in
I am using CarrierWave with MiniMagick to resize and uploaded file. I have the
I created a database using GoDaddy's online database manager, but I can't query it
I'm using Rails3 and have created a simple document table with a document_name (string)
I'm looking at using Silverlight to create a multi-file uploader for one of our

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.