Flo
Flo

Reputation: 21

Force Symfony to save the embedForm

I have a SymfonyForm which has a embedForm. I want to save the embedForm if filled even if the main Form is empty. So that the User can upload as many Photos as he wants for one foo objekt. But i dont know how to force symfony to save the embedForm ?!

Schema

Foo:
 columns:
 title: {type: string(255), notnull: true}

Photo:
 columns:
  foo_id: {type: integer}
  filename: {type: string(255), notnull: true}
  caption: {type: string(255), notnull: true}
 relations:
  Foo:
   alias: Foo
   foreignType: many
   foreignAlias: Photos
   onDelete: cascade

FooPhotoForm:

$photo = new Photo();
$photo->Foo = $this->getObject();
$photoForm = new PhotoForm($photo);
$this->embedForm('newPhoto', $photoForm);

PhotoForm:

$this->useFields(array('filename', 'caption',));
$this->validatorSchema['filename'] = new sfvalidatorFile(...);

Action:

 protected function processForm(sfWebRequest $request, sfForm $form)
    {
 $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
 if ($form->isValid())
 {
     $cms = $form->save();

 }
 else
 {
     if(caption and filename not empty)
     {
  $form->saveEmbeddedForms();
     }
     else
     {
  $this->getUser()->setFlash('error', 'The item has not been saved due to some errors.', false);
     }
 }

Upvotes: 2

Views: 908

Answers (1)

guiman
guiman

Reputation: 1334

Forcing seems to me like you are trying to hack into normal execution to make things work, i dont really recommend you to do it, but here are some options:

  • my first approach to that problem would be using some ajax to allow users to save an specific part and not sending the hole bunch of empty fields.
  • You should modifie your processForm (general admin generator method that takes care of saving form objects) action to retrieve the embbedForm information from the request, create a instance of the embbed form and bind the request information (only the one of the embbed form) so validation could take place. If its valid then save it.

Check the admin-generator code (stored in cache) in actions like create or update, it could help you visualize a bit more the idea.

Upvotes: 1

Related Questions