如何在Symfony中获取textarea的值

时间:2011-05-11 10:58:56

标签: symfony1

我似乎无法获得textarea的输入文本。当我这样做时:

die($request->getPostParameter('comment')) 

输出单词“array”。当我print_r()时它确实显示textarea是一个数组,它的值存储在数组中。但我不知道如何获得这个值,所以我可以将它放入表中的字段中。

@ greg0ire:我这样做是因为我试图将数据保存到两个不同的表中。我的html页面显示的表单实际上由两个不同类/模型的两个表单组成。我已设法将所有字段保存到两个表,但注释字段除外。然后我尝试获取值并意识到它是一个数组,想知道这是否是导致我的数据不能保存的原因。这就是我提出这个问题的原因。我问过另一个question,它解释了背景。

这些是单击提交按钮时运行的功能

   public function executeUpdateInlineForm(sfWebRequest $request)
{

  $overdueInvestigation = Doctrine_Core::getTable('investigation')->find( $request->getParameter('id'));

    $investigationForm = new investigationInlineForm($overdueInvestigation);
    $commentForm=new commentForm();


    $investigationForm->bind($request->getParameter($investigationForm->getName()), $request->getFiles($investigationForm->getName()));
    $commentForm->bind($request->getParameter($commentForm->getName()), $request->getFiles($commentForm->getName()));

    $this->processInlineForm($investigationForm, $commentForm);

  }

protected function processInlineForm(sfForm $investigationForm, sfForm $commentForm)
{

   if ($investigationForm->isValid())
      {
         $investigation = $investigationForm->save();  

         $comment = $commentForm->updateObject();
         $comment->setInvestigation_id($investigationForm->getObject()->getId());
         $comment->setComment($commentForm->getObject()->getComment());
         $comment->setuserId($investigationForm->getObject()->getCreatedUserId());
         $comment->setDateEntered(time());
         $comment->save();
         $this->redirect('investigation/overdue/');
     }


}

1 个答案:

答案 0 :(得分:2)

你可以简单地将$request->getPostParameter('comment')存储在数组中并在此数组上使用array_pop(),但我认为最好理解为什么要获取数组。我认为textarea的名称必须是comment[],而它应该只是comment

<强>更新

在阅读更新和您的其他问题后,您的字段似乎需要具有以下命名约定:

<input type="text" name="investigation[field1]"/>
<input type="text" name="investigation[field2]"/>
<input type="text" name="investigation[field3]"/>
<input type="text" name="comment[content]"/>

在表单的窗口小部件架构上使用setNameFormat()方法来实现此目的,然后将调查表单绑定到investigation请求参数,并将评论表单绑定到comment参数,你没事。

祝你好运!