Yii Framework Forum: file upload problem - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

file upload problem Rate Topic: -----

#1 User is offline   dimis283 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 277
  • Joined: 24-January 09

Posted 26 July 2009 - 12:50 PM

I create with crud comand line the forms for the "lan" table ->lan_code,lan_flag.
lan_flag will be an image so I change a litle the code
The problem is that even I write something for code and a file for the image there is a validation error.
What is wrong?
actionCreate-validation rule etc
 public function rules()
    {
        return array(
           array('lan_flag', 'required', 'on'=>'Create'),
            array('lan_flag', 'file', 'types'=>'jpg, gif, png'),

            array('lan_code', 'required'),
        );
    }
public function afterSave() {
    $caminhoArquivo = Yii::app()->basePath . '/file/' . $this->lan_flag;
    CUploadedFile::getInstance($this,'lan_flag')->saveAs($caminhoArquivo); }


public function actionCreate()
	{
		$model=new lan;
       
		if(isset($_POST['lan']))
		{ 
			$model->attributes=$_POST['lan'];
              $model->lan_flag=CUploadedFile::getInstance($model,'lan_flag');
print_r($_POST);
            print_r($model); exit();
			if($model->save())
				$this->redirect(array('show','id'=>$model->lan_code));
		}
		$this->render('create',array('model'=>$model));
	}

form
<?php echo CHtml::beginForm(array('method'=>'post','enctype'=>'multipart/form-data')); ?>

<?php echo CHtml::errorSummary($model); ?>

<div class="simple">
<?php echo CHtml::activeLabelEx($model,'lan_code'); ?>
<?php echo CHtml::activeTextField($model,'lan_code'); ?>
</div>

<div class="simple">
<?php echo CHtml::activeLabelEx($model,'lan_flag'); ?>
<?php echo CHtml::activeFileField($model,'lan_flag'); ?>
</div>

<div class="action">
<?php echo CHtml::submitButton($update ? 'Save' : 'Create'); ?>
</div>

<?php echo CHtml::endForm(); ?>

Posted Image
0

#2 User is offline   dimis283 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 277
  • Joined: 24-January 09

Posted 26 July 2009 - 01:50 PM

I manage to solve the problem, I want at update the file not to be required, how can I do that?Now if I set empty the file, the field will be null at table
Is there any method to resize the file at upload?
Dimis
Posted Image
0

#3 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,892
  • Joined: 04-October 08
  • Location:DC, USA

Posted 29 July 2009 - 03:53 PM

set "allowEmpty" option of your "file" validation rule to be true.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users