【问题标题】:Symfony add embedded collection prototype empty stringSymfony 添加嵌入式集合原型空字符串
【发布时间】:2015-05-05 19:12:43
【问题描述】:

我可能错过了一些愚蠢的东西。但我正在关注这本食谱http://symfony.com/doc/current/cookbook/form/form_collections.html,并希望有一个链接/按钮来为品牌添加更多过滤器。但是data-prototype 属性总是空的,尽管 symfony 没有错误。

这是我的表格

<?php

namespace DB\ScoreboardBundle\Form\Type;

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

use DB\ScoreboardBundle\Form\Type\FilterType;

class BrandType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('name' , 'text' , array('label' => 'Brand Name'))
            ->add('email', 'email')
            ->add('publicKey' , 'text' , array('read_only' => true))
            ->add('privateKey' , 'text' , array('read_only' => true))
            ->add('enabled' , 'checkbox' , array('label' => 'Enabled?' , 'required' => false))
            ->add('filters' , 'collection' , array(
                'type' => new FilterType(),
                'allow_add' => true));
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'DB\ScoreboardBundle\Entity\Brand',
        ));
    }

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

过滤器

<?php

namespace DB\ScoreboardBundle\Form\Type;

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

class FilterType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('field' , 'text')
            ->add('value', 'text')
            ->add('operator' , 'choice' , array('choices' => array(
                '=' => 'Equals (=)',
                '>=' => 'Greater than / equals (>=)',
                '<=' => 'Less than / equals (<=)',
                '>' => 'Greater than (>)',
                '<' => 'Less than (<)'
            )))
            ->add('enabled' , 'checkbox' , array('label' => 'Enabled?' , 'required' => false));
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'DB\ScoreboardBundle\Entity\Filter',
        ));
    }

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

这是我的控制器。

<?php

namespace AppBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use DB\ScoreboardBundle\Entity\Brand;
use DB\ScoreboardBundle\Entity\BrandManager;
use DB\ScoreboardBundle\Form\Type\BrandType;

class BrandController extends Controller
{
    public function viewAction(Brand $brand)
    {
        $form = $this->createForm(new BrandType() , $brand , array('action' => $this->generateUrl('admin_brand_edit' , array('id' => $brand->getId()))))
            ->add('save' , 'submit')
            ->add('new_keys' , 'submit' , array('label' => 'Save with New Keys'));

        return $this->render('AppBundle:Brand:view.html.twig' , array('brand_form' => $form->createView()));
    }
}

这是风景

{% block content %}
    <div id="add-admin" class="container">

        <div class="panel col-md-8">
            <div class="panel-heading">
                <h3 class="panel-title">Brand</h3>
            </div>
            <div class="panel-body">
                {% if brand_form is defined %}
                    {{ form_start(brand_form) }}
                    {{ form_widget(brand_form)}}
                    <ul class="filters" data-prototype="{{ form_widget(brand_form.filters.vars.prototype)|e }}">
                    </ul>
                    {{ form_end(brand_form) }}
                {% endif %}
            </div>
        </div>
    </div>
{% endblock %}

但最终data-prototype 在我执行检查元素时始终为空。如果我执行{{ dump(brand_form) }},我可以看到过滤器内容存在。那么我遗漏了哪些小细节呢?

【问题讨论】:

    标签: javascript php symfony


    【解决方案1】:

    找到了答案。没看过这部分。

    如果您一次呈现整个“标签”子表单(例如 form_row(form.tags)),则原型自动可用 外部 div 作为 data-prototype 属性,类似于您看到的 以上。

    因此,由于我将表单作为一个整体进行渲染,它已经在生成原型,显然 symfony 阻止了它的双重渲染。

    【讨论】:

    猜你喜欢
    • 2019-03-05
    • 1970-01-01
    • 2019-08-30
    • 2020-06-07
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多