logo
Ask your Symfony questions! Pay money and get answers fast! (more info)

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.

$10
Saving images in embedded form (Propel)

Hi I'm using Symfony 1.4 and I'm trying to save images in an embedded form.

I followed the tutorial here which worked really well.

The problem comes when I try to upload an image.

I have a Gallery and GalleryImage(individual item) that I have the embed form functionality in.

My GalleryForm.class.php
class GalleryForm extends BaseGalleryForm
{
public function configure()
{
$this->configureWidgets();
$this->configureValidators();
$this->configureEmbeddedForms();

parent::configure();
}

protected function configureWidgets()
{
unset($this['id']);
}

protected function configureValidators()
{
$this->validatorSchema['name']->setOption('required', true);
$this->validatorSchema['description']->setOption('required', true);
}

protected function configureEmbeddedForms()
{
$imagesForm = new sfForm();
foreach($this->getObject()->getGalleryImages() as $image)
{
$imagesForm->embedForm('images_'.$image->getId(), new GalleryImageForm($image));
}
// add one blank image gallery form
$imagesForm->embedForm('new_1', new GalleryImageForm());

// embed the image gallery form
$this->embedForm('images', $imagesForm);

$this->widgetSchema->setNameFormat('gallery[%s]');
}

public function bind(array $taintedValues = null, array $taintedFiles = null)
{
// over-ride the bind()
// add all the new image fields that don't exist (because they were added dynamically)
foreach($taintedValues['images'] as $key => $newTodo)
{
if (!isset($this['images'][$key]))
{
$this->addNewGalleryImage($key);
}
}

parent::bind($taintedValues, $taintedFiles);
}

public function addNewGalleryImage($name)
{
// this is called when we need to dynamically add the form when adding new images
$this->embeddedForms['images']->embedForm($name, new GalleryImageForm());

// re-embed the form
$this->embedForm('images', $this->embeddedForms['images']);
}

public function saveEmbeddedForms($con = null, $forms = null)
{
// loop through all the embeeded forms and save each one
foreach($this->embeddedForms['images']->getEmbeddedForms() as $imageForm)
{
if (!$imageForm->getObject()->getGalleryId())
{
$imageForm->getObject()->setGalleryId($this->getObject()->getId());
}
}

parent::saveEmbeddedForms($con, $forms);
}
}


My GalleryImageForm.class.php
class GalleryImageForm extends BaseGalleryImageForm
{

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


$this->widgetSchema ['filename'] = new sfWidgetFormInputFileEditable ( array (
'label' => 'Image',
'file_src' => '/uploads/images/' . $this->getObject()->getFilename(),
'is_image' => true, 'edit_mode' => ! $this->isNew () && $this->getObject ()->getFilename (),
'template' => '<div id="image_upload">%file%<br />%input%<br /> <div>%delete% %delete_label%</div></div>'
));

$this->validatorSchema['name'] = new sfValidatorString();
$this->validatorSchema ['image'] = new sfValidatorFile ( array (
'required' => false,
'path' => '/uploads/images/' . $this->getObject()->getFilename(),
'mime_types' => 'web_images',
));
}
}


How could I change my code to allow such image uploads?

Thanks

terrid | 12/01/10 at 7:19am Edit
Tutorial: How to assign prize money

Previous versions of this question: 12/02/10 at 7:20am

(7) Responses

See a threaded view of answers?

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

  • avatar
    Last edited:
    12/01/10
    7:47am
    Alex Zgorzhelsky says:

    Please, check the permissions on uploads/images/ folder. Image upload is done in sfValidatorFile class and it looks alright to me

  • avatar
    Last edited:
    12/01/10
    7:47am
    Martin Palacio says:

    You can try this excellent tutorial from the More with symfony book:

    http://www.symfony-project.org/more-with-symfony/1_4/en/06-Advanced-Forms

  • avatar
    Last edited:
    12/01/10
    11:15pm
    Ivan Rey says:

    What is the problem. Do you have an error or does it do somthing.

    Previous versions of this answer: 12/01/10 at 11:15pm

  • avatar
    Last edited:
    12/02/10
    2:13am
    Loban Rahman says:

    Yeah, please give some more details on what's happening.

  • avatar
    Last edited:
    12/01/10
    7:54am
    terrid says:

    The folder uploads/images doesn't exist, but I thought If it tried writing to it, then there would be a an error telling me the folder doesn't exist or is unwriteable

  • avatar
    Last edited:
    12/01/10
    11:14pm
    Ivan Rey says:

    Could we see your action ?

  • avatar
    Last edited:
    12/02/10
    6:09am
    terrid says:

    Ok so here is a snippet from my Gallery admin action:


    public function executeEdit(sfWebRequest $request)
    {
    $this->form = $this->getGalleryForm();
    }

    public function executeGalleryProcess(sfWebRequest $request)
    {
    $this->form = $this->getGalleryForm();

    if($request->isMethod('post'))
    {
    $this->form->bind($request->getParameter('gallery'), $request->getFiles('gallery'));
    if($this->form->isValid())
    {
    $this->form->save();
    $this->redirect('gallery/edit?id='.$this->form->getObject()->getId());
    }
    }

    $this->redirect('gallery/edit?id='.$this->form->getObject()->getId());
    }

    protected function getGalleryForm()
    {
    $gallery = GalleryPeer::retrieveByPK($this->getRequest()->getParameter('id'));
    if (!$gallery)
    {
    $gallery = new Gallery();
    }

    return new GalleryForm($gallery);
    }
    .............................

    I could also post the editSuccess.php code as well, although that is just the rendering of the form elements.

This question has expired.





Current status of this question: Community pot



Please log in to add additional discourse to this page.





Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.