Reputation: 21
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
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:
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