Twig 模板的 ContextErrorException 和 Twig 语法错误问题 - Symfony (FOSUserBundle)

ContextErrorException and Twig syntax error issue with a twig template - Symfony (FOSUserBundle)

我在 Symfony 上使用 FOSUserBundle 进行用户注册。我遵循了所需的步骤并继续按照指示覆盖模板。

我遇到了这些错误:

ContextErrorException: Notice: Undefined variable: test

其次是:

Twig_Error_Syntax: An exception has been thrown during the compilation of a template ("Notice: Undefined variable: test") in "FOSUserBundle:Registration:register_content.html.twig"

这是有问题的 register_content.html.twig:

{% trans_default_domain 'FOSUserBundle' %}

<form action="{{ path('fos_user_registration_register') }}" {{ form_enctype(form) }} 
      method="POST" 
      class="fos_user_registration_register">
    {{ form_widget(form) }}
    <div>
        <input type="submit" value="{{ 'registration.submit'|trans }}" />
    </div>
</form>

注册类型:

<?php


namespace DEA\CourriersBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;

class RegistrationType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('nom')
            ->add('prenom')
            ->add('fonction')
            ->add('telephone');
    }

    public function getParent()
    {
        return 'fos_user_registration';
    }

    public function getName()
    {
        return 'app_user_registration';
    }
}

我没有覆盖控制器。你可以找到它 here.

我什至找不到所说的 "test" 变量。感谢任何帮助,我已经为此苦苦挣扎了一整天。

我找到了一个解决方案,尽管我不明白它为什么有效。

我删除了

{{ form_enctype(form) }}`

由于某种原因导致错误。也许我的文件编码与它有关?

解决办法是将Twig更新到最新版本。

该错误是由于 Twig 中的错误引起的,该错误已在 Twig 1.21.2 中修复:https://github.com/twigphp/Twig/pull/1801

form_enctype(form) 在 Symfony 2.3 中已弃用,并将在 Symfony 3.0 中删除。您应该改用 form_start()。