【问题标题】:my entity does not update throw a form我的实体没有更新抛出一个表格
【发布时间】:2014-03-07 14:15:10
【问题描述】:

我有一个实体“组”,有名称和描述。 我创建了一个用于更新两者的表单,但是当我单击提交时,值设置为NULL

我还尝试创建一个新实体。但在这种情况下,它们的值在字段中设置为默认值。

这是我的代码:

我的表格:

  <form method="post" class="form-signin" {{ form_enctype(form) }}>
    <input id="name" name="form[name]" required="required" value="{{ group.name }}"></input>
    <textarea id="description" name="form[description]" >{{ group.description }}</textarea>
    <input class="btn btn-lg btn-primary btn-block" type="submit" id="_submit" name="_submit" value="submit" />
  </form>

我的控制器:

  public function updateParametersAction()
  {
    $user = $this->get('security.context')->getToken()->getUser();

    $repository = $this->getDoctrine()
                   ->getManager()
                   ->getRepository('MyBundle:Groups');

    $group = $repository->findOneByIdUser($user->getId());

    $form = $this->createFormBuilder($group)
                 ->add('name',        'text')
                 ->add('description', 'textarea')
                 ->getForm();

    $request = $this->get('request');

    if ($request->getMethod() == 'POST') {    
      $em = $this->getDoctrine()->getManager();
      $em->persist($group);
      $em->flush();
    }

    return $this->render('MyBundle:Client:updateParameters.html.twig', array(
      'group' => $group,
      'form' => $form->createView()
      ));
  }

怎么了?

【问题讨论】:

    标签: php symfony entity twig bundle


    【解决方案1】:

    您必须将请求绑定到表单:

    // (...)
    
    $form = $this->createFormBuilder($group)
                 ->add('name',        'text')
                 ->add('description', 'textarea')
                 ->getForm();
    
    $form->handleRequest($request);
    
    // (...)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-16
      • 1970-01-01
      • 1970-01-01
      • 2015-04-14
      • 1970-01-01
      • 1970-01-01
      • 2018-08-14
      • 1970-01-01
      相关资源
      最近更新 更多