Subscribe to our feed

Symfony Experts

Symfony Experts
If you have an urgent question for a symfony-related issue, this is the place to ask.


Stack Overflow

The old fashioned way


March 30, 2010 – 8:06pm Symfony 1.2 doctrine file upload in form

Here are the required modifications for to a form class that contains a file upload field in symfony 1.2 using doctrine (and it probably works with propel, too).

class NewsletterForm extends BaseNewsletterForm
  public function configure()
    $this->widgetSchema['file'] = new sfWidgetFormInputFileEditable(array(
      'file_src' => '/uploads/'.$this->getObject()->getfile(),
      'is_image'  => true,
      'edit_mode' => !$this->isNew(),
      //'template'  => '<div>%file%<br />%input%<br />%delete% %delete_label%</div>',
    $this->validatorSchema['file'] = new sfValidatorFile(array(
      'required' => false,
      'path' => sfConfig::get('sf_upload_dir')
    $this->validatorSchema['file_delete'] = new sfValidatorPass();

If you want to override the file name generated for your uploaded file, add a ‘generateXXXFileName()’ method to your model’s class where XXX is the column name that contains the file name (in this example, our column is “file”, so the method name is ‘generateFileFilename’. It takes a single parameter, which is an instance of sfValidatedFile.

class Newsletter extends BaseNewsletter
  public function generateFileFileName($file)
    return $file->getOriginalName();

One Response to Symfony 1.2 doctrine file upload in form

  1. gotnospirit says:

    I just wanna point that using the original name -as it- is not a good (storage) strategy because a filepath could then been linked to more than one item (imagine you test your form with the same image). And when you will delete the first item, the file will be physically erased while it is still referenced by the second item.
    You should then consider adding a unique key to your filename, so multiple uploads -using the same file- won’t ends with the same filepath.