我做了一个非常小的testformular来自我教授自定义表格类在Synfomy中。我没有输出,我没有发现错误。我希望有人可以帮助我。
我来自班级:
<?php
namespace AppBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\TextType;
class PfsFormType extends AbstractType
{
public function buildTheForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name')
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\PfsForm'
));
}
}
我的实体:
<?php
namespace AppBundle\Entity;
class PfsForm
{
private $name;
private $frage;
/**
* @return mixed
*/
public function getFrage()
{
return $this->frage;
}
/**
* @param mixed $frage
*/
public function setFrage($frage)
{
$this->frage = $frage;
}
/**
* @return mixed
*/
public function getName()
{
return $this->name;
}
/**
* @param mixed $name
*/
public function setName($name)
{
$this->name = $name;
}
}
控制器应调用表格:
<?php
namespace AppBundle\Controller;
use AppBundle\Entity\PfsForm;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use AppBundle\Form\PfsFormType;
class IndexController extends Controller
{
public function startAction()
{
$pfsForm = new PfsForm();
$form = $this->createForm(PfsFormType::class,$pfsForm );
return $this->render(
'index.html.twig',array(
'form' => $form->createView(),
)
);
}
}
枝条模板:
enter code here
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{{ dump() }}
{{ form_start(form) }}
{{ form_widget(form) }}
{{ form_end(form) }}
</body>
</html>
这是Twig转储输出:
array:2 [▼
"form" => FormView {#408 ▼
+vars: array:24 [▼
"value" => PfsForm {#361 ▶}
"attr" => []
"form" => FormView {#408}
"id" => "pfs_form"
"name" => "pfs_form"
"full_name" => "pfs_form"
"disabled" => false
"label" => null
"label_format" => null
"multipart" => false
"block_prefixes" => array:3 [▶]
"unique_block_prefix" => "_pfs_form"
"translation_domain" => null
"cache_key" => "_pfs_form_pfs_form"
"errors" => FormErrorIterator {#419 ▶}
"valid" => true
"data" => PfsForm {#361 ▶}
"required" => true
"size" => null
"label_attr" => []
"compound" => true
"method" => "POST"
"action" => ""
"submitted" => false
]
+parent: null
+children: array:1 [▼
"_token" => FormView {#424 ▶}
]
-rendered: false
}
"app" => AppVariable {#435 ▼
-tokenStorage: TokenStorage {#239 ▶}
-requestStack: RequestStack {#200 ▶}
-environment: "dev"
-debug: true
}
]
谢谢你的帮助 米莎
答案 0 :(得分:2)
您尝试使用表单buildTheForm
的方法不是正确的名称!
尝试使用buildForm
,这是正确的:
class PfsFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name')
;
}
}