我有一个带有 embedForm 的 SymfonyForm。即使主表单为空,我也想保存 embedForm 如果已填写。这样用户就可以为一个 foo 对象上传任意数量的照片。但我不知道如何强制 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);
照片形式:
$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);
}
}