I created a form type in SearchType.php file. But when I try to display the form in search.html.twig file, none of the inputs is displayed. I tried to write {{ form(form) }} in the html.twig file and see what I have. I find myself with an empty text input instead of two selects as expected.
How do I display inputs created in my form type ?
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 >
<div >
{{ form_start(form) }}
{{ form_widget(form.bigcity) }}
{{ form_widget(form.category) }}
{{ form_end(form) }}
</div>
</div>
{% endblock %}
I have this error :
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()
]);
}
}
CodePudding user response:
Same issue as your previous post the namespace is wrong in the controller the searchType should be App\Form\SearchType instead of Symfony\Component\Form\Extension\Core\Type\SearchType in your EventsController class