Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6769273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:10:24+00:00 2026-05-26T15:10:24+00:00

I need to upload images using symfony, but ive not been able to do

  • 0

I need to upload images using symfony, but ive not been able to do it using my form…

A simplified model is:

Offer:
  columns:
    id:
    name:
    pic:
    flyer:
    description:
      .
      .
      .
  relations:
    Picture:
      local: pic
      foreign: id
      type: one
    Picture_2:
      class: Picture
      local: flyer
      foreign: id
      type: one
Picture:
  columns:
    id:
    path:
    name:
      .
      .
      .

Now, im using a form that extends OfferForm, since I need my form to have file widgets instead of a choice widget for the fields ‘pic’ and ‘flyer’. In the saving process, then, i need to create two instances of ‘Picture’ to create the two Picture objects associated to this Offer.

I have not managed to find good and complete documentation on uploading files… or at least not to my case… every tutorial or article magically uses the $form->save() method, and all goes fine!, but i have had multiple errors while doing this…

this is my form class:

class myOfferForm extends OfferForm {

  protected $statusChoices = array(
                                   'A' => 'Active',
                                   'E' => 'Expired',
                                   );
  protected $validStatus = array('A','E');

  public function configure() {
    parent::configure();

    $this->setWidgets(array(
       'id'          => new sfWidgetFormInputHidden(),
       'name'        => new sfWidgetFormInputText(),
       'picFile'     => new sfWidgetFormInputFileEditable(array(
                             'file_src'     => $this->getObject()->getPicFileSrc(),
                             'is_image'     => true,
                             'with_delete'  => !is_null($this->getObject()->getPicFileSrc())
                                                               )),
       'flyerFile'   => new sfWidgetFormInputFileEditable(array(
                             'file_src'     => $this->getObject()->getFlyerFileSrc(),
                             'is_image'     => true,
                             'with_delete'  => !is_null($this->getObject()->getFlyerFileSrc())
                                                               )),
       'from'        => new sfWidgetFormDate(array(
                             'format'       => '%day%/%month%/%year%',
                             'can_be_empty' => false,
                             'default'      => date('Y/m/d')
                                                  )),
       'to'          => new sfWidgetFormDate(array(
                             'format'       => '%day%/%month%/%year%',
                             'can_be_empty' => false,
                             'default'      => date('Y/m/d')
                                                  )),
       'description' => new sfWidgetFormTextarea(),
       'status'      => new sfWidgetFormChoice(array(
                             'choices' => $this->statusChoices)),
       'products'    => new sfWidgetFormDoctrineChoice(array(
                             'model'        => 'Product',
                             'table_method' => 'getActivesOrderedByName',
                             'add_empty'    => 'Check associated products',
                             'multiple'     => true,
                                                             )
                                                       ),
    ));
    $this->widgetSchema->setLabels(array(
        'id'          => '',
        'name'        => 'Name *:',
        'picFile'     => 'Picture *:',
        'flyerFile'   => 'Flyer *:',
        'from'        => 'Valid From *:',
        'to'          => 'Valid To *:',
        'description' => 'Description :',
        'status'      => 'Status *:',
        'products'    => 'Associated Products :',
    ));
    $this->setValidators(array(
        'id'          => new sfValidatorChoice(array(
                             'choices' => array($this->getObject()->get('id')),
                             'empty_value' => $this->getObject()->get('id'),
                             'required' => false,
                                                     )),
        'name'        => new sfValidatorString(),
        'picFile'    => new sfValidatorFile(array(
                             'required'    => false,
                             'mime_types'  => 'web_images',
                             'path'        => WebPromocion::getStaticDirPath().'/',
                             'validated_file_class' => 'OfferValidatedFile',
                                                   )),
        'flyerFile'   => new sfValidatorFile(array(
                             'required'    => false,
                             'mime_types'  => 'web_images',
                             'path'        => WebPromocion::getStaticDirPath().'/',
                             'validated_file_class' => 'OfferValidatedFile',
                                                   )),
        'from'        => new sfValidatorDate(),
        'to'          => new sfValidatorDate(),
        'description' => new sfValidatorString(),
        'status'      => new sfValidatorChoice(array(
                             'choices' => $this->validStatus,
                             'required' => true,
                                                     )),
        'products'    => new sfValidatorDoctrineChoice(array(
                                                 'required' => false,
                                                 'model'    => 'Product',
                                                 'column'   => 'id',
                                                 'multiple' => true, )),
    ));
    $this->validatorSchema['fotoFile_delete'] = new sfValidatorPass();
    $this->validatorSchema['flyerFile_delete'] = new sfValidatorPass();
    $this->widgetSchema->setIdFormat('offer_form_%s');
    $this->widgetSchema->setNameFormat('offer[%s]');
  }
}

The OfferValidatedFile class:

class OfferValidatedFile extends sfValidatedFile {
  /**
   * Generates a random filename for the current file, in case
   * it already exists.
   *
   * @return string A convenient name to represent the current file
   */
  public function generateFilename()
  {
    $filename = $this->getOriginalName().$this->getExtension($this->getOriginalExtension());
    if (file_exits(WebPromocion::getStaticDirSrc().$filename)) {
      return sha1($this->getOriginalName().rand(11111, 99999)).$this->getExtension($this->getOriginalExtension());
    } else {
      return $filename;
    }
  }
}

And, in my action, I am doing, along with other stuff:

$this->form->save()

There is a problem. The Offer object does not have any existing Picture object to be associated with by the time the form is saved….

I think the main problem is that i want to use one form to handle submition of information related to two different objects.

So, what am I doing wrong? what am I not doing? Does someone knows a complete documentation on this subject that i could use? Is there a clean symfonian way to do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T15:10:25+00:00Added an answer on May 26, 2026 at 3:10 pm

    This documentation is for the version of symfony and doctrine you’re using. Unfortunately, rather than outlining initial setup for you, they include an installer script. Note that this type of setup is outlined elsewhere in both ‘A Gentle Introduction to Symfony‘ as well as in other parts of ‘More with Symfony‘ for version 1.4. Fortunately there is also a fairly verbose look at refactoring the script’s form class in the first link which I think would benefit you quite a bit as well – It explains the model’s processing of the form (Where you appear to be having issues) quite well, which may make debugging a little less mystifying for you.

    I recommend giving it a good read. I followed this for a project a few months ago and came out of it without any issues.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to upload images using FileUpload without postback(using Ajax).I tried many examples.But in
I'm using Symfony 1.4.4 and Doctrine and I need to upload an image on
I need a simple code to upload images to mySQL using PHP... short! snippet...
I'm trying to upload images using Graph API Batch Request, but i'm unable to
I have a problem to upload an image using Symfony. I have a form
I am, using a cakephp form helper to upload images to my server. The
i am using paperclip gem to upload images on the server side, i need
I need to upload a given image using Amazon S3 I have this PHP:
How to upload a image file using ajax by using prototype.js plugin, I need
hy, i need a little help here: i use SWFupload to upload images! in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.