这是以下问题的扩展 Symfony 2.7. How to get/set value of the Form field after Form submission?。
我需要在表单提交后在控制器中设置表单字段的数据。此功能不起作用。如何启用它?如何以其他方式实现这一目标?
... \ SRC \ MeetingBundle \表格\ EventType.php
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('starttimeInt', 'hidden', array('data' => '0',) )
->add('endtimeInt', 'hidden', array('data' => '0',) )
.. \ SRC \ MeetingBundle \控制器\ EventMapController.php
$starttimeInt=$dateObj->getTimestamp();
$form->get('starttimeInt')->setData($starttimeInt);
$starttimeIntSet=$form->get('starttimeInt')->getData();
print_r($starttimeIntSet); // prints 0 instead of the timestamp
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush(); // starttimeInt column contains 0 instead of timestamp, which i want to add to the form after form submission
答案 0 :(得分:1)
答案是将数据设置为实体:
.. \ SRC \ MeetingBundle \控制器\ EventMapController.php
/**
* Creates a new Event entity.
*
* @Route("/createjsmapV2", name="ev_jsMap_createV2")
* @Method("POST|GET")
* @Template("MeetingBundle:event:ev_jsMap_new.html.twig")
*/
public function createJsMapAction(Request $request)
$entity = new Event();
$form = $this->createCreateJsMapForm($entity);
$form->handleRequest($request);
if ($form->isValid()) {
$starttimeInt=$dateObj->getTimestamp();
// form->get('starttimeInt')->setData($starttimeInt); // this will not work, you will find 0 in the database as it is the default value in FormType
//here you can set the data to any field you want to modify, after the user submitted the form
$entity->setStarttimeInt($starttimeInt);
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();