我在SearchType.php文件中创建了一个表单类型。但是当我试图在search.html.twig文件中显示表单时,没有任何输入被显示。我试图在html.twig文件中写入{{ form(form)}}并查看我所拥有的内容。我发现自己的文本输入是空的,而不是预期的两个选择。
如何显示在我的表单类型中创建的输入?
SearchType.php
<?php
namespace App\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
class SearchType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('bigcity', ChoiceType::class, [
'choices' => [
'Je sélectionne une ville' => '',
'Paris, France' => 'paris'
]
])
->add('category', ChoiceType::class, [
'choices' => [
"Je sélectionne un type d'activité" => '',
'Eat Zpeak !' => 'eatzpeak',
'Party Zpeak !' => 'partyzpeak',
'Run Zpeak !' => 'runzpeak',
'Art Zpeak !' => 'artzpeak',
]
])
->add('save', SubmitType::class)
;
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => null
]);
}
}
search.html.twig
{% extends 'base.html.twig' %}
{% block title %}Liste des sorties et des activités !{% endblock %}
{% block main %}
<div class="position-relative">
<div class="position-absolute top-50 start-50 translate-middle">
{{ form_start(form) }}
{{ form_widget(form.bigcity) }}
{{ form_widget(form.category) }}
{{ form_end(form) }}
</div>
</div>
{% endblock %}
我有这个错误:
Neither the property "bigcity" nor one of the methods exist and have public access in class
EventsController.php
<?php
namespace App\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Form\Extension\Core\Type\SearchType;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class EventsController extends AbstractController
{
#[Route('/search', name: 'search')]
public function search(Request $request)
{
$form = $this->createForm(SearchType::class);
return $this->render('front/search.html.twig', [
'form' => $form->createView()
]);
}
}
1条答案
按热度按时间k10s72fa1#
与您之前的帖子相同的问题,控制器中的命名空间错误,在EventsController类中,searchType应该是App\Form\SearchType,而不是Symfony\Component\Form\Extension\Core\Type\SearchType