Symfony4 Twig模板错误:变量“ widget”不存在

时间:2018-11-20 11:46:00

标签: symfony twig

我正在获取Twig_Error_Runtime enter image description here

我正在尝试查找所犯的错误,但看不到任何错误……也许我正在查看错误的文件,但是我尝试查找在发生此错误之前所做的更改的所有地方

这是我的树枝代码:

{% 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,
    ));
}

这是TimeType:

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

1 个答案:

答案 0 :(得分:1)

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

因此,请尝试将TimeType重命名为TicketTimeType之类的东西。

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

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