简体   繁体   English

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

[英]Symfony4 Twig template error: Variable “widget” does not exist

I am getting Twig_Error_Runtime 我正在获取Twig_Error_Runtime 在此处输入图片说明

I am trying to find the mistake I've made, but I can't see any... maybe I am looking at the wrong file, but I've tried looking everywhere I've done changes before this error appeared. 我正在尝试查找所犯的错误,但看不到任何错误……也许我正在查看错误的文件,但是在出现此错误之前,我已经尝试在所有进行了更改的地方进行查找。

This is my twig code: 这是我的树枝代码:

{% 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 %}

I've also made changes in Entities Time & Ticket, but I don't think so this is connected somehow. 我还更改了实体时间和票务,但我不认为这是某种联系。

Here's my TicketType Form: 这是我的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,
    ));
}

And this is TimeType: 这是TimeType:

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

Because the classname of your formtype TimeType is the same as the Symfony TimeType ( https://symfony.com/doc/current/reference/forms/types/time.html ) the formtheme layout tries to render the symfony type instead of yours. 因为您的表单类型TimeType的类名与Symfony TimeType( https://symfony.com/doc/current/reference/forms/types/time.html )相同,所以formtheme布局会尝试呈现symfony类型而不是您的symfony类型。 You can see the symfony TimeType has an option called widget so the formtype is expecting this type. 您可以看到symfony TimeType有一个名为widget的选项,因此formtype需要这种类型。

So try to rename your TimeType to something else like TicketTimeType . 因此,尝试将您的TimeType重命名为TimeType等其他TicketTimeType

Or you can rename your block prefix like this in your TimeType : 或者,您可以在TimeType这样重命名块前缀:

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

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

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