php - Symfony3/PHP7 表单提交不起作用

标签 php mysql forms symfony

我正在尝试创建一个可以执行数据库查询并在数据库中插入一些数据的简单表单。但问题是表单未提交(?),$form->isSubmitted() 始终为 false。

use Symfony\Component\Form\Extension\Core\Type\TextType as TextTypeForm;
use Symfony\Component\Form\Extension\Core\Type\SubmitType as SubmitTypeForm;

public function renderFormAction(Request $request){
    $task = new Task();
    $task->setTitle('Keyboard');
    $task->setDescription('Ergonomic and stylish!');

    $form = $this->createFormBuilder($task)
        ->setMethod("POST")
        ->add('title', TextTypeForm::class)
        ->add('description', TextTypeForm::class)
        ->add('save', SubmitTypeForm::class, array('label' => 'Create Task'))
        ->getForm();



    try {
        $form->handleRequest($request);
    } catch (\Exception $e) {
        echo "failed : ".$e->getMessage();
    }

    if ($form->isSubmitted()) {
        $data = $form->getData();
        $em->persist($task);
        $em->flush();

        return $this->redirectToRoute('task_success');
    }

    return $this->render('eisenhover/addtask.html.twig', array(
        'form' => $form->createView(),
    ));
}

任务类:

<?php

namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
 * @ORM\Entity
 * @ORM\Table(name="task")
 */
class Task
{
/**
 * @ORM\Id
 * @ORM\Column(type="integer")
 * @ORM\GeneratedValue
 */
private $id;

/**
 * @ORM\Column(type="string")
 */
private $title;

/**
 * @ORM\Column(type="string")
 */
private $description;




/**
  * @ORM\Embedded(class = "Status")
  */
private $status;


public function addTask(string $title, string $desc, bool $urgent, bool $important){
    $em = $this->getDoctrine()->getManager();

    $product = new Task();
    $product->setTitle($title);
    $status = new Status($urgent,$important);
    $product->setStatus($status);
    $product->setDescription($desc);

    $em->persist($product);

    $em->flush();
} // "addtask"           [POST] /create
/**
 * Get id
 *
 * @return int
 */
public function getId()
{
    return $this->id;
}

/**
 * Set title
 *
 * @param string $title
 *
 * @return Task
 */
public function setTitle($title)
{
    $this->title = $title;

    return $this;
}

/**
 * Get title
 *
 * @return string
 */
public function getTitle()
{
    return $this->title;
}

/**
 * Set description
 *
 * @param string $description
 *
 * @return Task
 */
public function setDescription($description)
{
    $this->description = $description;

    return $this;
}

/**
 * Get description
 *
 * @return string
 */
public function getDescription()
{
    return $this->description;
}

/**
 * Set status
 *
 * @param integer $status
 *
 * @return Task
 */
public function setStatus($status)
{
    $this->status = $status;

    return $this;
}

/**
 * Get status
 *
 * @return int
 */
public function getStatus()
{
    return $this->status;
}

最佳答案

在我看来你定义了错误的类型 http://symfony.com/doc/current/reference/forms/types.html .
TextTypeForm 和 SubmitTypeForm 是未知类型。

$form = $this->createFormBuilder($task)
    ->setMethod("POST")
    ->add('title', TextTypeForm::class)
    ->add('description', TextTypeForm::class)
    ->add('save', SubmitTypeForm::class, array('label' => 'Create Task'))
    ->getForm();

应该是

$form = $this->createFormBuilder($task)
    ->setMethod("POST")
    ->add('title', TextType::class)
    ->add('description', TextType::class)
    ->add('save', SubmitType::class, array('label' => 'Create Task'))
    ->getForm();

关于php - Symfony3/PHP7 表单提交不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47533876/

相关文章:

php - 计算数据库中没有新行的百分比

php - mysqli_query不起作用。缺少连接?

php - 我怎样才能在 postgres 上转换?

mysql - 表中的多值属性

html - 如何在 react 中管理表单内输入字段的颜色?

Django 表单缓存或重新加载问题

php - 将特定日期的多个时差相加

php - MySQL 数据库 : How far to Normalize/Queries VS Join/Unique Index

mysql - C# 插入所有行还是不插入?

java - 将 Servlet 错误消息重定向到 JSP 后保存表单数据