简体   繁体   中英

Symfony3/PHP7 Form submit doesn't work

I am trying to create a simple form which can execute a database query and insert some data in database. But the problem is that the form isn't submitting(?), the $form->isSubmitted() is always 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(),
    ));
}

Task class:

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

}

Looks to me you defining the wrong types http://symfony.com/doc/current/reference/forms/types.html .
TextTypeForm and SubmitTypeForm are unknown types.

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

Should be

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM