您没有从正确的角度解决问题。如果应该有一个主标签,那么这个属性不应该加在Tag实体本身,而应该加在包含它的实体中!
我说的是与具有 tags 属性的表单相关的 data_class 实体。这是应该具有 mainTag 属性的实体。
如果定义正确,这个新的 mainTag 属性将不是布尔值,因为它将包含一个 Tag 实例,因此不会与复选框条目相关联。
所以,在我看来,您应该有一个包含您的实例的 mainTag 属性和一个包含所有其他标签的 tags 属性。
这样做的问题是您的收藏字段将不再包含主标签。因此,您还应该创建一个特殊的 getter getAllTags,它将您的主标签与所有其他标签合并,并将您的集合定义更改为:
$builder->add('allTags', 'collection', array(
'type' => new TagType(),
'label' => false,
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false
));
现在,您可能会问,我们如何添加单选框?为此,您必须生成一个新字段:
$builder->add('mainTag', 'radio', array(
'type' => 'choice',
'multiple' => false,
'expanded' => true,
'property_path' => 'mainTag.id', // Necessary, for 'choice' does not support data_classes
));
这些是基础知识,但它只会从这里变得更加复杂。这里真正的问题是表单的显示方式。在同一个字段中,您混合了集合的通常显示和该集合的父表单的选择字段的显示。这将迫使您使用form theming。
为了留出一些可重用空间,您需要创建一个自定义字段。关联的data_class:
class TagSelection
{
private mainTag;
private $tags;
public function getAllTags()
{
return array_merge(array($this->getMainTag()), $this->getTags());
}
public function setAllTags($tags)
{
// If the main tag is not null, search and remove it before calling setTags($tags)
}
// Getters, setters
}
表单类型:
class TagSelectionType extends AbstractType
{
protected buildForm( ... )
{
$builder->add('allTags', 'collection', array(
'type' => new TagType(),
'label' => false,
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false
));
// Since we cannot know which tags are available before binding or setting data, a listener must be used
$formFactory = $builder->getFormFactory();
$listener = function(FormEvent $event) use ($formFactory) {
$data = $event->getForm()->getData();
// Get all tags id currently in the data
$choices = ...;
// Careful, in PRE_BIND this is an array of scalars while in PRE_SET_DATA it is an array of Tag instances
$field = $this->factory->createNamed('mainTag', 'radio', null, array(
'type' => 'choice',
'multiple' => false,
'expanded' => true,
'choices' => $choices,
'property_path' => 'mainTag.id',
));
$event->getForm()->add($field);
}
$builder->addEventListener(FormEvent::PRE_SET_DATA, $listener);
$builder->addEventListener(FormEvent::PRE_BIND, $listener);
}
public function getName()
{
return 'tag_selection';
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'TagSelection', // Adapt depending on class name
// 'prototype' => true,
));
}
}
最后,在表单主题模板中:
{% block tag_selection_widget %}
{% spaceless %}
{# {% set attr = attr|default({})|merge({'data-prototype': form_widget(prototype)}) %} #}
<ul {{ block('widget_attributes') }}>
{% for child in form.allTags %}
<li>{{ form_widget(form.mainTag[child.name]) }} {{ form_widget(child) }}</li>
{% endfor %}
</ul>
{% endspaceless %}
{% endblock tag_selection_widget %}
最后,我们需要将它包含在您的父实体中,即最初包含标签的那个:
class entity
{
// Doctrine definition and whatnot
private $tags;
// Doctrine definition and whatnot
private $mainTag;
...
public setAllTags($tagSelection)
{
$this->setMainTag($tagSelection->getMainTag());
$this->setTags($tagSelection->getTags());
}
public getAllTags()
{
$ret = new TagSelection();
$ret->setMainTag($this->getMainTag());
$ret->setTags($this->getTags());
return $ret;
}
...
}
并以您的原始形式:
$builder->add('allTags', new TagSelection(), array(
'label' => false,
));
我承认我提出的解决方案很冗长,但在我看来它是最有效的。你想要做的事情在 Symfony 中是不容易做到的。
您还可以注意到评论中有一个奇怪的“原型”选项。在您的情况下,我只是想强调“集合”的一个非常有用的属性:原型选项包含您的集合的一个空白项目,并带有要替换的占位符。这允许使用 javascript 在集合字段中快速添加新项目,更多信息here。