Symfony 3.3 - Twig 运行时错误,带有连字符形式的小部件名称

Symfony 3.3 - Twig runtime error with hyphenated form widget name

我正在尝试将 BeelabRecaptcha2Bundle 集成到我的项目中。因此,我想将 reCaptcha 字段本身命名为 g-recaptcha-response。当我将它添加到我的 Twig 模板时,出现以下错误:

Uncaught PHP Exception Twig_Error_Runtime: "Neither the property "g" nor one of the methods "g()", "getg()"/"isg()"/"hasg()" or "__call()" exist and have public access in class "Symfony\Component\Form\FormView"

这是有问题的 Twig 代码:

<div>
    {{ form_widget(form.g-recaptcha-response) }}
</div>

我怎样才能让它工作?

尝试

<div>
    {{ form_widget(form['g-recaptcha-response']) }}
</div>

<div>
    {{ form_widget(attribute(form, 'g-recaptcha-response')) }}
</div>

您可以在 docs 中阅读更多相关信息。