2
我有一個有embedForm的SymfonyForm。即使主窗體爲空,我也想保存embedForm。以便用戶可以上傳儘可能多的照片,因爲他需要爲一個foo對象。但我不知道如何強制symfony保存embedForm?!Force Symfony來保存embedForm
架構
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(...);
操作:
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);
}
}