Symfony 4 Twig 模板错误 : Variable "widget" does not exist

标签 symfony twig

我收到 Twig_Error_Runtime enter image description here

我试图找到我所犯的错误,但我看不到任何错误...也许我正在查看错误的文件,但我尝试查看在出现此错误之前所做的所有更改.

这是我的 Twig 代码:

{% extends 'base.html.twig' %}
{% block body %}
    <div class="container">

        {% form_theme form 'bootstrap_4_layout.html.twig' %}
        {{ form_start(form) }}

        <br>

        Name {{ form_widget(form.name) }}
        Price {{ form_widget(form.price) }}
        Available {{ form_widget(form.available) }}
        Date {{ form_widget(form.date) }}
        <div class="row js-ticket-time-wrapper"
             data-prototype="{{ form_widget(form.times.vars.prototype)|e('html_attr') }}"
             data-index="{{ form.times|length }}">
            {% for time in form.times %}
                <div class="col-xs-4 js-ticket-time-item">
                    <a href="#" class="js-remove-time pull-right">
                        <span class="fa fa-close"></span>
                    </a>
                    {{ form_row(time.name) }}
                </div>
            {% endfor %}
            <a href="#" class="js-ticket-time-add">
                <span class="fa fa-plus-circle"></span>
                Add another Time
            </a>
        </div>
        <button type="submit" class="btn btn-primary" formnovalidate>Save</button>
        {{ form_end(form) }}
    </div>

{% endblock %}
{% block js %}
    {{ parent() }}
    <script>
        jQuery(document).ready(function() {
            var $wrapper = $('.js-ticket-time-wrapper');
            $wrapper.on('click', '.js-remove-time', function(e) {
                e.preventDefault();
                $(this).closest('.js-ticket-time-item')
                    .fadeOut()
                    .remove();
            });
            $wrapper.on('click', '.js-ticket-time-add', function(e) {
                e.preventDefault();
                // Get the data-prototype explained earlier
                var prototype = $wrapper.data('prototype');
                // get the new index
                var index = $wrapper.data('index');
                // Replace '__name__' in the prototype's HTML to
                // instead be a number based on how many items we have
                var newForm = prototype.replace(/__name__/g, index);
                // increase the index with one for the next item
                $wrapper.data('index', index + 1);
                // Display the form in the page before the "new" link
                $(this).before(newForm);
            });
        });
    </script>
{% endblock %}

我还在实体时间和票证中进行了更改,但我认为这没有某种联系。

这是我的 TicketType 表单:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('name',TextType::class)
        ->add('price',MoneyType::class)
        ->add('available',IntegerType::class)
        ->add('date',DateType::class)
        ->add('times', CollectionType::class, array(
            'entry_type' => \App\Form\TimeType::class,
            'allow_delete' => true,
            'by_reference' => false,
            'allow_add' => true,
    ));
}

这是时间类型:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('name', TextType::class);
}

最佳答案

因为您的 formtype TimeType 的类名与 Symfony TimeType ( https://symfony.com/doc/current/reference/forms/types/time.html ) 相同,所以 formtheme 布局会尝试呈现 symfony 类型而不是您的类型。您可以看到 symfony TimeType 有一个名为 widget 的选项,因此 formtype 需要这种类型。

因此,请尝试将您的 TimeType 重命名为 TicketTimeType 等其他名称。

或者您可以在 TimeType 中重命名 block 前缀:

public function getBlockPrefix()
{
    return "ticket_time_type";
}

关于Symfony 4 Twig 模板错误 : Variable "widget" does not exist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53392329/

相关文章:

php - AppKernel.php 奇怪的行为

php - Doctrine 如何在使用 addSelect 查询时在一行中获得结果

php - 如何避免在 Symfony 3 中通过 JSON 返回用户密码

php - FOSUserBundle 模板覆盖不适用于 Symfony Flex

Twig:while-workaround

javascript - Twig 到 Js 的数据不完整数组

php - Doctrine DBAL 如果不存在则插入

php - Twig/PHP - 使用 Replace 或 Regex 格式化字符串

symfony - 使用 Symfony2 获取 Twig 模板中的环境名称

forms - 原型(prototype)字段在表单(表单)之后为空,但在表单(表单)之前设置时设置 symfony