2017-11-28 10 views
0

データベースクエリを実行してデータベースにデータを挿入できる単純なフォームを作成しようとしています。しかし問題は、フォームが提出していないということです(?)、$form->isSubmitted()は常にfalseです。Symfony3/PHP7フォーム提出が機能しません

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クラス:

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

}

+0

は、あなたがしようとして渡すことを確認しているべきでしょうか? –

+0

私はtryを使用していなかったが、まだ動作していなかったし、何のエラーもない。 – shaelex

+0

それを削除し、handleRequestを試して見たことがありませんでした。 私たちを助ける何か他にありますか? –

答えて

4

間違った種類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(); 
+0

申し訳ありませんが、私は言及しませんでしたが、タイプはうまくいきます。 TextTypeフォームとしてのTextType。 SubmitTypeとしてのSubmitType。 – shaelex

+0

それの有用性は何ですか? –

+0

Doctrine \ DBAL \ Types \ TextTypeを使用してTextTypeを2回宣言することはできません。 – shaelex

関連する問題