首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >表单提交后如何设置数据?

问表单提交后如何设置数据?
EN

Stack Overflow用户
提问于 2015-12-16 05:47:40
回答 1查看 53关注 0票数 1

这是以下问题Symfony 2.7. How to get/set value of the Form field after Form submission?的扩展。

在表单提交后,我需要在控制器中设置表单字段的数据。此功能不起作用。如何启用?如何通过其他方式实现这一点?

...\src\MeetingBundle\Form\EventType.php

代码语言:javascript
复制
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

代码语言:javascript
复制
$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
EN

回答 1

Stack Overflow用户

发布于 2015-12-16 06:07:26

答案是将数据设置为实体:

..\src\MeetingBundle\Controller\EventMapController.php

代码语言:javascript
复制
/**
 * 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();
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34300027

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档