Symfony表单-如何更改CollectionTypes项目标签 [英] Symfony Forms - How to Change a CollectionTypes Items labels

查看:109
本文介绍了Symfony表单-如何更改CollectionTypes项目标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我关于stackoverflow的第一个问题;直到现在,我只是为我的问题寻找答案并找到了答案.但是现在看来,我有一个人,或者至少没有一个人在这里偶然发现的.

This is my first question on stackoverflow; until now I just looked for answers for my issues and found them. But now it seems that I have one that nobody or at least no one here stumbled across.

关于我的问题:

在我的班级FieldType extends AbstractType中,我想更改CollectionType项目的标签.

In my Class FieldType extends AbstractType I want to change the labels of CollectionType items.

这适用于所有包含的项目,但我想单独设置标签:

This works for all contained items, but I want to set the labels individually:

$translations = $builder->create("translations", CollectionType::class, array(
            "label" => "Translations",
            "type" => TranslationType::class,
            "entry_options" => array(
                'label' => "THIS IS A TEST"
            )
        ));

在这里,我将新类型添加到CollectionType并尝试设置每个项目的标签:

Here I add new types to the CollectionType and try to set each item's label:

foreach ($this->field->getTranslations() as $translation) {
            /** @var Language $language */
            $iso2 = strtolower($translation->getLanguage()->getIso2());
            $translation = $builder->create("translation_{$iso2}", TranslationType::class, array(
                'label' => $iso2,
                )
            );
            $translations->add($translation);
        }
$builder->add($translations);

但是它们没有显示在模板中;我想在这里,集合的索引显示为(0,1,...). (请参见渲染的翻译FormView )

But they are not displayed in the template; I suppose that here the indices of the Collection are shown (0,1,...). (see Rendered Translations FormView)

这是我的TranslationType:

class TranslationType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add("label");
        $builder->add("placeholder");
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => Translation::class,
        ));
    }
}

推荐答案

我是通过搜索完全相同的问题到达这里的.

I got here by searching for exactly the same question.

0,1,...可能是您实体的ArrayCollection的键.但这无济于事.

The 0,1,... probably are the keys of ArrayCollection of your entity. But that doesn't help.

下面的方法不是很漂亮,但是应该可以使用.我将在这个线程中寻找更好的建议...

The methods below are not pretty, but they should work. I'll be watching this thread for better suggestions...

方法1

在您的FormType类中,添加:

In your FormType class, add:

use Symfony\Component\Form\FormView;
use Symfony\Component\Form\FormInterface;

...


/** 
 * this gets called in the final stage before rendering the form
 */   
public function finishView(FormView $view, FormInterface $form, array $options)
{
    foreach ($view['translations']->children as $childView)
    {
        // here you have access to all child form vars, assigned entities, etc
        $childView->vars['label'] = ''; // set label here
    }
}

方法2:

只需根据 http://symfony.com/doc来自定义模板/current/cookbook/form/form_customization.html

代替{{ form_label() }},使用其他:)

就像我说的那样,它并不漂亮,但希望对您有所帮助.

As I said, it is not pretty, but hope it helps.

这篇关于Symfony表单-如何更改CollectionTypes项目标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆