Symfony 1.1 - editing uploaded file with sfForm

Lately, i a had problem with editing uploaded file.
I had an object with field “photo” that was linking to the uploaded image.
When I was editing the object and leaving the input file blank (not selecting any other image) the form saved NULL in database.

I was looking for solution on many forums and in documentation but i didn’t found anything that could help me.
Finally, i solved the problem (maybe it’s not the best way, but it’s working really good).

I also added checkbox for “delete image” option. If we are uploading new file, the old one is auto deleted.

Maybe it would help anyone.

The most important thing is to add this function to BaseFormPropel.class.php:


<?php

/**
   * Sets a value for a form field.
   *
   * @param string $field	The field name
   * @param mixed  $value	The default value
   */
    public function setValue($field, $value){

    	if(!in_array($field, array_keys($this->values))){

    		throw new sfException(sprintf('Unkown field" "%s" in "%s" object.', $field, get_class($this)));
    	}

    	$this->values[$field] = $value;
    }
?>

Then we could call that method in save function. MyForm.php:


<?php

  public function configure()
  {
		$this->widgetSchema->setNameFormat('profile[%s]');

    	 # Edit default labels
		$this->widgetSchema->setLabels(
			array(
				'name' => 'Image name',
				'description'  => 'Image description',
				'deletefile'  => 'Delete image?',
			)
		);

    	 # Edit default widgets
    	$this->widgetSchema['deletefile'] = new sfWidgetFormInputCheckbox();
    	$this->widgetSchema['description'] = new sfWidgetFormTextarea();
  		$this->widgetSchema['photo'] = new sfWidgetFormInputFile();

    	 # Edii defaults validators
    	$this->validatorSchema['name'] = new sfValidatorString(array(),
    			array('required' => 'Title is required'));
		$this->widgetSchema['deletefile'] = new sfWidgetFormInputCheckbox();

    	$this->validatorSchema['photo'] = new sfValidatorFile(
    			array('required' => false,
						'max_size'	 => '1024000',
						'mime_types' => array('image/jpeg')
					),
				array(
					'mime_types'	=> 'The file you submit is not a valid format. Please upload a JPG, GIF or PNG image file',
					'max_size'		=> 'File size limit is 1MB, please make your file smaller')
					);

  }

  public function save($con = null) {

        $path = sfConfig::get('sf_upload_dir') .'/';

		if($file = $this->getValue('photo')){

    		 # We can also delete file from serwer calling deleteFile method
    		//$this->deleteFile( $this->getObject()->getPhoto() );

			$filename = md5( $file->getOriginalName() . time() . rand( 0, 99999 ) ) . $file->getExtension( $file->getOriginalExtension() );

  			$file->save($path.$filename);

		}else if( $this->getValue('deletefile') ) {

			$this->setValue('photo', null);

			//$this->deleteFile( $this->getObject()->getPhoto() );
		}else{

			$this->setValue('photo', $this->getObject()->getPhoto());
		}

        return parent::save($con);
    }

?>

And in my action.class.php:


<?php

    	if($request->isMethod('post') && $request->hasParameter('profile')){

    		$this->form->bindAndSave($request->getParameter('profile'), $request->getFiles('profile'));

    	}

?>

FileTestSuccess.php:


<form action="<?php echo url_for('test/testMyForm') ?>" method="POST" enctype="multipart/form-data">

<table>

	<?php echo $form; ?>

		<tr>
			<td colspan="2">
				<input type="submit" value="<?php echo 'Upload'; ?>"/>
			</td>
		</tr>

</table>

</form>

P.S. Sorry for my English :)

1 Response to “Symfony 1.1 - editing uploaded file with sfForm”


  1. 1 tyx

    Thanks to your post, I fix this bug !

  1. 1 Symfony 1.2 - sfForm - Yet another useful function, part 2 | Projektant - Programista PHP

Leave a Reply

PodglÄ…d komentarza:




About me:

  • PHP programmer
  • Symfony developer
  • Zend framework developer

Categories: