activeFileField

Using activeFileField in my form, it outputs a hidden field which I assume is to be used when updating and the image is not changed. The problem here is that the value is always empty.

Also, if I put the following in the rules of my model I get an error:


array('image', 'file', 'types'=>'jpg, gif, png'),

The error:


Fatal error: Call to a member function getError() on a non-object in C:\wamp\www\yii\framework\validators\CFileValidator.php on line 146

Ok, I looked and see that the hidden field is just a fudge for something else. Why not make it useful, since I had to create another hidden field for "previous_image" when in update mode?

This is how I do it, if anybody else finds it useful (saving the file name to the database in my controller update action):




        if($model->image=CUploadedFile::getInstance($model,'image')) {

           $model->image->saveAs('/mypath/images/' . CUploadedFile::getInstance($model,'image')->name);

        } else {

          $model->image = $_POST['MyClass']['previous_image'];  // from the hidden field

        }



Anybody have a clue on the validation error?

Hi guys,

I have de same problem, the hidden field always return a empty value.

I did the sugestion of Backslider, but not work, now the form save a empty value on database instead of maintaining the existing value.

suggestions?

Thanks.

here are my actions:




  public function actionCreate()

  {

    $piece = new Repertory;

    $piece->scenario = 'create';

    $this->registration($piece, $_POST['Repertory']);

    $this->render('create', array('repertory' => $piece));

  }

  

  public function actionUpdate()

  {

    $piece = $this->loadpiece();

    $this->registration($piece, $_POST['Repertory']);    

    $this->render('update', array('repertory' => $piece));

  }


  private function registration($model, $form)

  {

    if(isset($form))

    {

      $model->attributes = $form;

      $model->media_file = CUploadedFile::getInstance($model, 'media_file');

      $model->thumb      = CUploadedFile::getInstance($model, 'thumb');

      if($model->save())

      {

        exec("mkdir -p " . Yii::app()->basePath . "/../files/repertory/media_file/{$model->id}/");

        exec("mkdir -p " . Yii::app()->basePath . "/../files/repertory/thumbs/{$model->id}/");

        $model->media_file->saveAs(Yii::app()->basePath . "/../files/repertory/media_file/{$model->id}/{$model->media_file->name}");

        $model->thumb->saveAs(Yii::app()->basePath . "/../files/repertory/thumbs/{$model->id}/{$model->thumb->name}");

        $this->redirect('/admin/repertory/list');

      }

    } 

  }




Unfortunately it is still not fixed in latest version of framework.

But it can be changed in core function

framework/web/helpers/CHtml.php

original code




	public static function activeFileField($model,$attribute,$htmlOptions=array())

	{

		self::resolveNameID($model,$attribute,$htmlOptions);

		// add a hidden field so that if a model only has a file field, we can

		// still use isset($_POST[$modelClass]) to detect if the input is submitted

		$hiddenOptions=isset($htmlOptions['id']) ? array('id'=>self::ID_PREFIX.$htmlOptions['id']) : array('id'=>false);

		return self::hiddenField($htmlOptions['name'],'',$hiddenOptions)

			. self::activeInputField('file',$model,$attribute,$htmlOptions);

	}



new code




	public static function activeFileField($model,$attribute,$htmlOptions=array(),$hiddenvalue='')

	{

		self::resolveNameID($model,$attribute,$htmlOptions);

		// add a hidden field so that if a model only has a file field, we can

		// still use isset($_POST[$modelClass]) to detect if the input is submitted

		$hiddenOptions=isset($htmlOptions['id']) ? array('id'=>self::ID_PREFIX.$htmlOptions['id']) : array('id'=>false);

		return self::hiddenField($htmlOptions['name'],$hiddenvalue,$hiddenOptions)

			. self::activeInputField('file',$model,$attribute,$htmlOptions);

	}



thuadm,thank u very much,this is big bug in the yiiframework,why qiang can’t fix this bug in the new vesion?I hope qiang can see this bug and fix it.

You can report that issue at Google Code Yii’s repository.