Symfony 4,当我尝试提交表单时,它不起作用

时间:2017-12-10 17:26:32

标签: php symfony symfony4

提前感谢那些想要帮助我的人。我正在学习symfony 4,我正在测试如何通过从表单中获取数据来更新数据库。所以我确保它在id的基础上寻找一个表,并且它使用正确的表单值填充表单。但是当我提交时,表格 - > isSubmitted条件永远不会被验证。你有什么建议吗?

    public function updateArticle(Request $request, $id)
  {
    $em = $this->getDoctrine()->getManager();
    $article = $em->getRepository(Article::class)->find($id);
    if (!$article) {
        throw $this->createNotFoundException(
            'No article found for id '.$id
        );
    }
    $articletext = $article->getArticle();
    $title = $article->getTitle();
    $image = $article->getFeatureimage();
    $category = $article->getCategory();
    $author = $article->getAuthor();
    $article->setArticle($articletext);
    $article->setTitle($title);
    $article->setFeatureimage($image);
    $article->setCategory($category);
    $article->setAuthor($author);
    $form = $this->createFormBuilder($article)
        ->add('article', TextareaType::class)
        ->add('title', TextType::class)
        ->add('featureimage', FileType::class, array('data_class' => null,'required' => true))
        ->add('category', TextType::class)
        ->add('author', TextType::class)
        ->add('save', SubmitType::class, array('label' => 'Inserisci articolo'))
        ->getForm();
    if ($form->isSubmitted()) {
        $article = $form->getData();
        print_r($article);
        return $this->redirectToRoute('blog');
      }
      else
        return $this->render('insert.html.twig', array(
          'form' => $form->createView(),
        ));
  }

1 个答案:

答案 0 :(得分:4)

您忘记了处理数据的行。 Look at the doc 在if条件之前添加此行:

//form creation as you did but it's better to construct the form via the FormType

$form->handleRequest($request);
if ($form->isSubmitted()){
//Do some stuff