SUBSCRIBE VIA RSS


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.

Topics

TWITTER

Stack Overflow


The old fashioned way

RECENT TUNES

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();
  }
}
Posted in  Web Development   |     |  delicious  Digg

One Response to “Symfony 1.2 doctrine file upload in form”

  1. Hello,
    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.

    By gotnospirit on Mar 31, 2010

Post a Comment

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word