这是以下问题的扩展Symfony 2.7。如何在提交表单后获取/设置表单字段的值。
表单提交后,我需要在控制器中设置表单字段的数据。此功能不起作用。如何启用它?如何通过其他方式实现这一点?
''src''MeetingBundle''Form''EventType.php
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('starttimeInt', 'hidden', array('data' => '0',) )
->add('endtimeInt', 'hidden', array('data' => '0',) )
''src''MeetingBundle''Controller''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
答案是将数据设置为实体:
''src''MeetingBundle''Controller''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();