データベースクエリを実行してデータベースにデータを挿入できる単純なフォームを作成しようとしています。しかし問題は、フォームが提出していないということです(?)、$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;
}
}
は、あなたがしようとして渡すことを確認しているべきでしょうか? –
私はtryを使用していなかったが、まだ動作していなかったし、何のエラーもない。 – shaelex
それを削除し、handleRequestを試して見たことがありませんでした。 私たちを助ける何か他にありますか? –