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

Saving images in embedded form (Propel) Symfony

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

I followed the tutorial [[LINK href="http://www.thatsquality.com/articles/embedding-child-forms-with-sfformpropel-a-practical-example"]]here[[/LINK]] 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

Answers (4)

2010-12-01

Alex Zgorzhelsky answers:

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


terrid comments:

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

2010-12-01

Martin Palacio answers:

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

2010-12-01

Ivan Rey answers:

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


Ivan Rey comments:

Could we see your action ?


terrid comments:

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.

2010-12-02

Loban Rahman answers:

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