Symfony3 / PHP7表单提交不起作用

时间:2017-11-28 14:23:27

标签: php mysql forms symfony

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

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;
}

}

1 个答案:

答案 0 :(得分:5)

在我看来你定义了错误的类型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();