Symfony 5.4 使用自定义类型渲染自定义模板表单 CollectionField

问题描述 投票:0回答:1

如何让该字段使用模板?

遵循文档似乎没有帮助。我错过了什么?

我正在尝试将其渲染到这个 CRUD 控制器字段上:

         $translationContents = CollectionField::new('translationKeys')
            ->setTemplatePath('admin/translation_content.html.twig')
            ->setFormTypeOptions([
                'entry_type' => TranslationType::class,
                'by_reference' => false,
            ]);

这是我的自定义类型:

class TranslationType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name', TextType::class, [
                'label' => 'Key',
            ])
            ->add('translationContent', CollectionType::class, [
                'entry_type' => TranslationContentType::class,
                'label' => false,
                'allow_add' => true,
                'allow_delete' => true,
            ]);
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => TranslationKey::class,
        ]);
    }
    public function getBlockPrefix()
    {
        return 'translation_key';
    }
}

这是在之前的类型中呈现的另一种自定义类型:

class TranslationContentType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('locale', EntityType::class, [
                'class' => Locale::class,
                'choice_label' => 'code', // Assuming your Locale entity has a 'code' property
                'label' => 'Locale',
            ])
            ->add('name', TextType::class, [
                'label' => 'Translation Content',
            ])
            ->add('tag', IntegerType::class, [
                'label' => 'tag',
                'attr' => [
                    'min' => 0,
                    'max' => 3
                ],
            ]);
    }

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

这是我正在尝试使用的模板。目前这只是一种调试是否正在渲染的方法。

{% block translation_key_widget %}
{{ dump('Template loaded!') }}
    {% for child in form.children|filter(child => not child.rendered) %}
        <div class="form-group">
            <p> test</p>
            {{ form_label(child) }}
            {{ form_widget(child) }}
            {{ form_help(child) }}
            {{ form_errors(child) }}
        </div>
    {% endfor %}
{% endblock %}

我已将其添加到 twig.yaml

    form_themes:
        - 'admin/translation_content.html.twig'
php symfony symfony-forms symfony5 easyadmin
1个回答
0
投票

我必须以这种方式在 CRUD 控制器上渲染模板:

    public function configureCrud(Crud $crud): Crud
    {
        return $crud
            // ->setFormThemes(['@EasyAdmin/crud/form_theme.html.twig'])
            ->setFormThemes(['admin/translation_content.html.twig', '@EasyAdmin/crud/form_theme.html.twig'])
        ;
    }
© www.soinside.com 2019 - 2024. All rights reserved.