Symfony表单无效且无法提交

时间:2017-10-03 14:58:01

标签: forms symfony submit

尝试提交表单时,它无效。该按钮根本没有做任何事情。所以我试图用我的东西抛弃一些东西:

if ($form->isValid() && $form->isSubmitted())

并意识到它甚至没有进入if语句,所以我认为我的表单有问题。但我还不知道它会是什么,所以如果有人能帮助我,我会很高兴的!

 /**
  * @Route("/document/bulkdeactivate", name="documentBundle_document_bulkDeactivate")
  * @Template()
  */
  public function bulkDeactivateAction(Request $request) {
      $em = $this->getDoctrine()->getManager();
      $selected_documents = $request->request->all();
      $form = $this->createForm(DocumentDeactivationType::class);
      $form->handleRequest($request);
        if ($form->isValid() && $form->isSubmitted()) {
          foreach($selected_documents as $document) {
          $documentR = json_decode(json_encode($document), true);
          dump($documentR);
            for($i=0; $i<count($documentR); $i++){
              $doc = $em->getRepository('DocumentBundle:Document')->findOneById($documentR[$i]);
              dump($doc);
              $doc->setActive(false);
              $em->persist($doc);
              $em->flush();
            }
          }
          $this->addFlash(
            'success',
            'The document has been deactivated!'
          );
          return $this->redirectToRoute('documentBundle_document_list');
        }
        return $this->render('DocumentBundle:Panels:ActivationPanel.html.twig', array(
          'form' => $form->createView(),
        ));
  }

我的表格类型:

public function buildForm(FormBuilderInterface $builder, array $options)
  {
    $builder
    ->add('documentlist', EntityType::class, array(
      'class' => 'DocumentBundle:Document',
      'choice_label' => 'name',
      'required' => false,
      'multiple' => true,
      'expanded' => false,
      'placeholder' => "Select Documents",
      'label' => 'label.document_list',
        ))

->add('submit', SubmitType::class, array(
  'label' => 'Submit',
  'attr' => array(
      'class' => 'btn btn btn-default',
  ),
  ));
  }

我的树枝模板的一部分:

     {% block content %}
    {{ form_start(form) }}
     {{ form(form.documentlist) }}
 {{ form(form.submit) }}
    {{ form_end(form) }}

    {% endblock content %}

表格一定有问题,您对我有什么建议吗?

2 个答案:

答案 0 :(得分:1)

实际上有几个问题:

  • 您无法在案例中添加未绑定到Symfony表单的提交按钮,因为Symfony无法正确识别它(因为缺少必须与SF的自动输入名称生成相匹配的名称)

在你的formBuilder中:

$builder->add('submit', SubmitType::class);
  • 您的表单名称不正确(&#34;表单&#34;)与Twig的form()功能冲突,您必须更改它,
  • 你可能有一个问题(没有测试,只是一个猜测)关于对Twig的form()的许多调用,因为这个函数转储整个表单内容,手动转储每个部分使用

您的{% block content %}应该有:

{{ form_widget(form.your_widget_name, attrs) }}

答案 1 :(得分:1)

我最终解决了我的问题: 1.正如@NaeiKinDus所指出的,我需要一个实际属于我的表单的提交按钮,另外我将表单名称更改为自定义 deactivationForm

{% block content %}
{{ form(deactivationForm) }}
{% endblock content %}

buildForm方法:

    $builder
->add('documentlist', EntityType::class, array(
  'class' => 'DocumentBundle:Document',
  'choice_label' => 'name',
  'required' => false,
  'multiple' => true,
  'expanded' => false,
  'placeholder' => "Select Documents",
  'label' => 'label.document_list',
  'translation_domain' => 'Documents',
  'query_builder' => function (EntityRepository $er) {
    return $er->createQueryBuilder('d')
      ->where('d.active = FALSE');
    },
))

->add('submit', SubmitType::class, array(
  'label' => 'Submit',
  'attr' => array(
      'class' => 'btn btn btn-default',
  ),
  ));

然后在我的控制器中:

/**
  * @Route("/document/bulkdeactivate", name="documentBundle_document_bulkDeactivate")
  * @Template()
  */
  public function bulkDeactivateAction(Request $request) {

    /*
      * GET DOCUMENT FROM DB
      */
      $em = $this->getDoctrine()->getManager();
      $selected_documents = $request->request->all();

      $deactivationForm = $this->createForm(DocumentDeactivationType::class);
      $deactivationForm->handleRequest($request);
        if ($deactivationForm->isValid() && $deactivationForm->isSubmitted()) {
          foreach($selected_documents as $document) {
          $documentR = json_decode(json_encode($document), true);
          dump(count($documentR['documentlist']));
            for($i=0; $i<count($documentR['documentlist']); $i++){
              $doc = $em->getRepository('DocumentBundle:Document')->findOneById($documentR['documentlist'][$i]);
              dump($documentR['documentlist'][$i]);
              $doc->setActive(true);
              $em->persist($doc);
              $em->flush();
            }
          }
          $this->addFlash(
            'success',
            'The selected document(s) have been deactivated!'
          );
          return $this->redirectToRoute('documentBundle_document_list');
          }

        return $this->render('DocumentBundle:Panels:ActivationPanel.html.twig', array(
          'deactivationForm' => $deactivationForm->createView(),
        ));
}

我试图访问错误的数组位置(我没有意识到ajax数据的解码向我发送了3个数组位置但我只想访问第一个'文档列表'然后从那个获取文档ID。