Reputation: 381
I made an very small testformular to teach myself with custom Form Classes in Synfomy. I dont have a output and i dont find the mistake. I hope someone can help me.
My From Class:
<?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'
));
}
}
my entity:
<?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;
}
}
The Controller which should call the form:
<?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(),
)
);
}
}
And the twig template:
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>
Thats the Twig dump output:
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
}
]
Thx for your help Micha
Upvotes: 1
Views: 79
Reputation: 1942
Method that you are trying to use with forms buildTheForm
is not the right name!
Try using buildForm
which is the correct one:
class PfsFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name')
;
}
}
http://symfony.com/doc/current/forms.html
Upvotes: 2