2011-01-25 27 views
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); 
    } 
} 

回答

1

強迫我看來,像你試圖侵入正常執行,使工作上的事情,我真的不建議你這樣做,但這裏有一些選擇:

  • 我的第一個方法,它的問題會使用一些ajax允許用戶保存一個特定的部分,而不是發送一堆空洞。
  • 您應該修改您的processForm(處理保存表單對象的一般管理生成器方法)操作以從請求中檢索embbedForm信息,創建嵌入表單的實例並綁定請求信息(僅嵌入一個embbedForm信息形式),因此可以進行驗證。如果它的有效然後保存它。

在創建或更新等操作中檢查管理生成器代碼(存儲在緩存中),它可以幫助您更直觀地瞭解這個想法。