简体   繁体   English

覆盖Symfony2表单主题时,变量“ form”不存在

[英]Variable “form” does not exist when overriding Symfony2 form theme

I am having an issue all of the sudden where I am getting the following error on my Symfony2 site: Variable "form" does not exist in SiteBackendBundle::layout.html.twig at line 2 . 我突然遇到一个问题,我在Symfony2网站上收到以下错误: Variable "form" does not exist in SiteBackendBundle::layout.html.twig at line 2

My SiteBackendBundle::layout.html.twig file looks like: 我的SiteBackendBundle :: layout.html.twig文件如下所示:

{% extends '::base.html.twig' %}
{% form_theme form 'SiteBackendBundle:Form:fields.html.twig' %}
{% block body %}
....
{% endblock %}

and my SiteBackendBundle:Form:fields.html.twig looks like this: 我的SiteBackendBundle:Form:fields.html.twig看起来像这样:

{% block money_widget %}
{% spaceless %}
  {{ block('form_widget_simple') | raw }}
{% endspaceless %}
{% endblock %}

My config.yml for Symfony/forms looks like this: 我的Symfony / forms的config.yml看起来像这样:

framework:
    #esi:             ~
    translator:      { fallback: en }
    secret:          %secret%
    router:
        resource: "%kernel.root_dir%/config/routing.yml"
        strict_requirements: %kernel.debug%
    form:            ~
    csrf_protection: ~
    validation:      { enable_annotations: true }
    templating:
        engines: ['twig']
        #assets_version: SomeVersionScheme
    default_locale:  "%locale%"
    trusted_proxies: ~
    session:         ~
    fragments:       ~

It's also weird because this was working before I ran php app/console cache:clear 这也很奇怪,因为这在我运行php app/console cache:clear之前就起作用了php app/console cache:clear

Why am I unable to extend the form without getting this error? 为什么在没有出现此错误的情况下无法扩展表格? This exact method is defined at http://symfony.com/doc/2.2/cookbook/form/form_customization.html#method-2-inside-a-separate-template http://symfony.com/doc/2.2/cookbook/form/form_customization.html#method-2-inside-a-separate-template中定义了此确切方法

If one or more of the twig files that extend your layout do not contain a form then presumably you won't be passing a form variable to them. 如果一个或多个扩展布局的树枝文件不包含表单,则可能不会将表单变量传递给它们。 You can get round this by updating your layout file as follows: 您可以通过如下更新布局文件来解决此问题:

{% if form is defined %}
{% form_theme form 'SiteBackendBundle:Form:fields.html.twig' %}
{% endif %}

I was having the same issue actually. 我实际上有同样的问题。 Not sure if this will help but it helped me. 不知道这是否会有所帮助,但对我有所帮助。 In your Controller you have to put in the array like I am doing inside of your return statement. 在您的Controller中,您必须像在return语句中一样在数组中放入数组。

public function PrefixController extends Controller\
{
    if(...){
    }
    return $this->render('BundleName:ControllerPrefix:TwigTemplate.html.twig', array('form' => $form->createView()));

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM