0
votes

I have everything working correctly and now I'm trying to work with form themes. This is my code to generate the form without a theme.

{% extends 'base.html.twig' %}
{% block body %}
{% include 'menu/menu.html.twig' %}
{% if addpost is defined %}
    <div id='add_post_form'>
        {{ form_start(addpost) }}
        {{ form_widget(addpost) }}
        {{ form_end(addpost) }}
    </div>
{% endif %}
{% endblock %}

But when I'm adding a form-theme with the following code

{% form_theme form 'form/form_div_layout.html.twig' %}

I get this error:

Variable "form" does not exist

When i execute this without the line, I'm getting the following error:

Unknown "form_help" function. Did you mean "form_rest", "form_end"?

the form_div_layout.html.twig contains the code found at github symfony twig form theme

At my config.yml I've added the following under the twig section,

form_themes:
        - 'form/form_div_layout.html.twig'

.

either not, i still have this error what is missing ???


My file structure

My file structure

1
Remove the theme from the config. It's not required, this way you make this theme global, to be used for every form. Unless you want to do that, if so you don't need the form_theme to be called. - emix
Your form variable is 'addpost'. - panche14
Also note that with {% form_theme form _self %} the error still same - Jordan Georgiadis

1 Answers

3
votes

If all your forms are going to use the same theme you only need to add the line in your config, but if you want a particular form theme in a particular template you can use the template tag.

The reason you're getting the 'form is not defined error' is because you don't have a variable called form passed the the template, your form variable is called addpost, so you need to use

{% form_theme addpost 'form/form_div_layout.html.twig' %}