2011-05-20 42 views
1

我需要顯示錶單以在顯示新聞故事的同一頁面上輸入博客文章。用戶正在輸入與故事相關的博客文章。如何訪問Symfony 1.4中顯示的表單中的相關對象

在我的博客的形式,我現在這樣做是爲了讓正在顯示的故事ID:

public function configure() 
{ 
    $this->setWidget('image_filename', new sfWidgetFormInputFileEditable(array(
    'file_src' => '/uploads/blogpost_images/thumbnails/thumb_'.$this->getObject()->image_filename, //displayed for existing photos 
    'edit_mode' => !$this->isNew(), 
    'is_image' => true, 
    'with_delete' => false, 

    ))); 

    $this->setValidator('image_filename', new sfValidatorFile(array(
     'mime_types' => 'web_images', 
     'required' => $this->isNew(), 
     'path' => sfConfig::get('sf_upload_dir').'/blogpost_images', 
     'validated_file_class' => 'BlogPostValidatedFile', 
    ))); 
    $this->setValidator('url', new sfValidatorUrl(array('required' => true))); 
    $this->setValidator('title', new sfValidatorString(array('required' => true))); 

    $this->setWidget('user_id', new sfWidgetFormInputHidden(array(),array(
     'value'=>sfContext::getInstance()->getUser()->getId()) 
      )); 

    // get the request params to access the notice ID and pass it back to the form for 
    // saving with the blog post 

    $params = sfContext::getInstance()->getRequest()->getParameterHolder(); 

    $this->setWidget('notice_id', 
      new sfWidgetFormInputHidden(array(),array(
       'value'=>$params->get('id')) 
        )); 

    $this->removeFields(); 


} 

是否有這樣做的更清潔的方式?從請求參數中獲取通知(新聞報道)的id是很冒險的。

UPDATE

我是從一個模態對話框通過AJAX實際發佈形式,並試圖保持整個請求notice_id值。我結合我與形式參數返回它顯示的錯誤之前:

public function executeModal(sfWebRequest $request) { 

    if ($request->isXmlHttpRequest()) { 
    //return $this->renderText('test'.$request->getParameterHolder()->getAll()); 
    $params = $request->getParameter('nb_blog_post'); 
    $form = new nbBlogPostForm(null,array('notice_id',$request->getPostParameter('notice_id'))); 

    $form->bind($params,$request->getFiles()); 


    if ($form->isValid()) { 
     $nb_blog_post = $form->save(); 
     $this->getUser()->setFlash('notice', 'Your blog post was successfully created'); 
     $this->redirect('@noticeboard'); 

    } else { 
     return $this->renderPartial('form',array('form'=>$form,'form_id'=>'blogpost')); 
    } 
    } 


    } 

我似乎無法得到與形式(這是一個隱藏字段)的約束notice_id。其他值綁定罰款。

I've also tried $form = new nbBlogPostForm(null,array('notice_id',$request->getPostParameter('nb_blog_post[notice_id]'))); 

進一步更新

在第一次通過表格配置方法依賴於在請求中notice_id是,我認爲這是形式通過AJAX再次創建時將其設置爲空。這修復了它:

$params = sfContext::getInstance()->getRequest()->getParameterHolder(); 
    if (($params->get('id'))) { 
    $this->setWidget('notice_id', 
      new sfWidgetFormInputHidden(array(),array(
       'value'=>$params->get('id')) 
        )); 
    } else { 
      $this->setWidget('notice_id', 
      new sfWidgetFormInputHidden()); 

    } 

如果有人有更清潔的方式請讓我知道。

回答

2

正確的方法是:

$form = new YourForm(null,array('notice_id' => $notice_id));

至於你提到的第一個參數必須是一個對象,以便空已定,那麼第二個參數應該包含變量傳遞到窗體

UPDATE

隨着新的信息提供。你有兩種選擇,第一種:

創建表單內像這樣的新方法:

public function setNoticeId($id){ 
$this->getWidget('notice_id')->setAttribute('value'),$id); 
} 

然後調用它的操作中創建表格後:

$this->form = new Form(); 
$this->form->setNoticeId($id); 

的第二個是不要在顯示錶單時設置通知ID,而是按照前面提到的方式在創建操作中傳遞此類參數:

public function executeCreate(sfWebRequest $request) 
{ 
$this->form = new Form(null, array('notice_id'=>$id)); 
$this->processForm($request,$this->form); 

} 

然後,你必須使用你的表單中保存方法(在beggining),並將其分配給所需的字段是這樣的:

$this->values['notice_id'] = $this->getOption('notice_id'); 

的冷杉方法是清潔的,但是當你需要一些數據,後者是有用這與您的對象無關,例如,如果您需要將帖子保存在用戶文件夾中,並且需要此用戶文件夾的名稱。

我希望這會有所幫助。

+0

感謝您的回覆 - 我已經更新了一些更多的信息。 – codecowboy 2011-05-21 06:32:42

1

我認爲最簡單便捷的方法是在創建新表單對象時傳遞表單元素的默認值。

像這樣:

$form = new YourForm(array('notice_id' => $notice_id)); 

這將自動爲您的表單元素的值。 (也適用於所有表單元素。)

問候。

+0

這看起來更乾淨 - 謝謝! – codecowboy 2011-05-20 12:31:10

+0

實際上表單隻接受一個對象,而不是一個數組 – codecowboy 2011-05-20 15:45:29

+2

嗯, 實際上擴展sfForm類的窗體(最可能是你的窗體)接受三個參數: __construct($ defaults,$ options,$ CSRFSecret) 來自Symfony API:http://www.symfony-project.org/api/1_4/sfForm#method___construct – 2011-05-20 19:31:02

相關問題