我想检索名为" message"的textarea的值。将它显示在FlashBag中#34;仅用于测试" ,但稍后会通过电子邮件发送。这是表单和控制器操作:
表格
<form action="{{ path('immobilier_delete', {'id': immobilier.id }) }}" method="post">
<a href="{{ path('immobilier') }}" class="btn btn-primary btn-md" role="button">Annuler</a>
<input type="submit" value="Supprimer" class="btn btn-danger" />
<textarea name="message"></textarea>
{{ form_rest(form) }}
</form>
动作控制器
public function deleteAction(Immobilier $immobilier)
{
$form = $this->createFormBuilder()->getForm();
$request = $this->getRequest();
if ($request->getMethod() == 'POST')
{
$form->bind($request);
if ($form->isValid())
{
$em = $this->getDoctrine()->getManager() ;
// here i'd like to make the textarea in $message
$em->remove($immobilier) ;
$em->flush() ;
$this->get('session')->getFlashBag()->add('info', 'message');
return $this->redirect( $this->generateUrl('immobilier'));
}
}
return $this->render('AgenceImmobBundle:Immobilier:delete.html.twig',
array(
'immobilier' => $immobilier,
'form' => $form->createView()
));
}
答案 0 :(得分:1)
要访问已发布的值,您真正需要做的就是使用$ _POST [&#39; key&#39;]
因此,在这种情况下,您的邮件位于:$ _POST [&#39; message&#39;]。
答案 1 :(得分:1)
您使用空表单,删除表单并直接使用
访问POST变量$request->request->get('message');
或将消息字段添加到表单。
<强>表格强>
<form action="{{ path('immobilier_delete', {'id': immobilier.id }) }}" method="post">
<a href="{{ path('immobilier') }}" class="btn btn-primary btn-md" role="button">Annuler</a>
<input type="submit" value="Supprimer" class="btn btn-danger" />
{{ form_widget(form) }}
</form>
<强>控制器强>
public function deleteAction(Immobilier $immobilier)
{
$form = $this->createFormBuilder()
->add('message', 'textarea')
->getForm();
$request = $this->getRequest();
if ($request->getMethod() == 'POST')
{
$form->bind($request);
if ($form->isValid())
{
$em = $this->getDoctrine()->getManager() ;
$em->remove($immobilier) ;
$em->flush() ;
$data = $form->getData();
$this->get('session')->getFlashBag()->add('info', $data['message']);
return $this->redirect( $this->generateUrl('immobilier'));
}
}
return $this->render('AgenceImmobBundle:Immobilier:delete.html.twig',
array(
'immobilier' => $immobilier,
'form' => $form->createView()
));
}