Anton
Anton

Reputation: 161

Symfony form not shown in template

In a Symfony 3.4 project, I just created a new form type ProductSearchType. The form fields are not shown in twig template. There are only the <form> tags, but nothing between them.

FormType:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('name', TextType::class, [
            'required' => false,
            'label' => 'Name',
        ])
        ->add('sizes', EntityType::class, [
            'required' => false,
            'label' => 'Bauform',
            'class' => ProductSize::class,
        ])
        ->add('description', TextType::class, [
            'required' => false,
            'label' => 'Beschreibung',
        ])
        ->add('price', EntityType::class, [
            'required' => false,
            'label' => 'Preiskategorie',
            'class' => ProductPrice::class,
        ])
        ->add('booster', EntityType::class, [
            'required' => false,
            'label' => 'Hörverlust',
            'class' => ProductBooster::class,
        ])
        ->add('brand', EntityType::class, [
            'required' => false,
            'label' => 'Marke',
            'class' => ProductBrand::class,
        ]);
}

Action:

public function index(Request $request): Response
{
    $products = $this->getDoctrine()->getRepository(Produkt::class)
        ->findBy([
            'showOnEmptySearch' => true,
        ], [
            'sortOnEmptySearch' => 'asc',
        ]);

    $searchForm = $this->createForm(ProductSearchType::class);
    $searchForm->handleRequest($request);

    $params = [
        'products' => $products,
        'search_form' => $searchForm->createView(),
    ];
    return $this->render('Products/index.html.twig', $params);
}

Twig part:

{% block template_filter %}
    <h1>
        Suche
    </h1>
    <div class="filter_box">
        {{ form(search_form) }}
    </div>
{% endblock %}

Upvotes: 0

Views: 753

Answers (1)

YahyaE
YahyaE

Reputation: 1057

I recommend to follow the documentation. On your example:

$searchForm = $this->createForm(ProductSearchType::class); should be:

$searchForm = $this->createForm(ProductSearchType::class)->getForm();.

Also on twig;

{{ form_start(search_form) }}
{{ form_widget(search_form) }}
{{ form_end(search_form) }}

Here is the documentation link. Welcome to Stackoverflow.

If you get answer to your question, don't forget to mark this answer as Accepted Answer.

Upvotes: 1

Related Questions