Symfony添加嵌入式集合原型空字符串

时间:2015-05-05 19:12:43

标签: javascript php symfony

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

这是我的表格

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

但是当我执行一个inspect元素时,data-prototype总是空的。如果我执行{{ dump(brand_form) }}我可以看到过滤器内容存在。那么我错过了什么细节呢?

1 个答案:

答案 0 :(得分:0)

找到答案。没有读过这部分。

  

如果你渲染整个&#34;标签&#34;一次子表格(例如   form_row(form.tags)),然后原型自动可用   外部div作为data-prototype属性,类似于你所看到的   上方。

所以,因为我整个渲染表单,它已经生成了原型,显然symfony阻止它进行双重渲染。