Symfony 2.8/3.0 升级:如何处理带有可变参数的表单类型? [英] Symfony 2.8/3.0 upgrade: how to deal with form types with variable parameters?

查看:29
本文介绍了Symfony 2.8/3.0 升级:如何处理带有可变参数的表单类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我将自定义表单类型创建为服务,正如 Symfony 文档.但我想要 2 个性别"自定义类型,带有 2 个不同的输入参数,我在 Symfony 2.7 中是这样做的:

Let's say I create custom form types as services, as it is described in the Symfony documentation. But I want 2 "gender" custom types, with 2 different input parameters, which I was doing like this in Symfony 2.7:

# app/config/config.yml
parameters:
    genders1:
        m: Male
        f: Female
    genders2: # This makes no sense at all, but it is for the example purpose!
        h: Horse
        t: Turtle

然后,我声明了 2 个这样的服务:

And then, I was declaring 2 services like this:

<!-- src/AppBundle/Resources/config/services.xml -->
<service id="app.form.type.gender1" class="AppBundle\Form\Type\GenderType">
    <argument>%genders1%</argument>
    <tag name="form.type" alias="gender1" />
</service>

<service id="app.form.type.gender2" class="AppBundle\Form\Type\GenderType">
    <argument>%genders2%</argument>
    <tag name="form.type" alias="gender2" />
</service>

如您所见,我将 same GenderType 类用于 2 种自定义表单类型(使用 gender1gender2 别名),我可以这样使用:

As you can see, I was using the same GenderType class for 2 custom form types (with the gender1 and gender2 aliases), which I could use like this:

$builder
    ->add('field1', 'gender1')
    ->add('field2', 'gender2');

这使我可以仅在具有不同输入参数的一个类 (GenderType) 中添加一些通用逻辑(在此示例中,我的可能性比 2 多得多).

This allowed me to add some common logic in only one class (GenderType) with different input parameters (I have many more possibilities than 2 in this example).

但是从 Symfony 2.8 开始,不推荐使用服务别名添加字段.类名必须作为第二个参数传递,如下所示:

But as of Symfony 2.8, adding a field using the service alias is deprecated. The class name has to be passed as the second argument instead, like this:

$builder->add('field1', GenderType::class)

那么如何区分我的 2 个服务(每个服务都没有相同的输入参数)?

So how can I make the difference between my 2 services (each of them not having the same input parameters)?

创建 Gender1TypeGender2Type 扩展抽象的 GenderType 类真的很痛苦,因为我必须创建很多带有空的类内容.

Creating Gender1Type and Gender2Type extending an abstract GenderType class would be really painful, as I would have to create a lot of classes with empty content.

你知道如何在 Symfony 2.8 中实现我的模式,保持服务具有不同的输入参数,但不创建很多类吗?

Do you have any idea on how to implement my pattern in Symfony 2.8, keeping services with different input parameters, but not creating a lot of classes?

推荐答案

好吧,在深入挖掘这个话题之后,有人已经在 PR 中直接提出了问题在 Symfony 2.8 中更改.

Well, after digging this topic a bit more, someone already asked the question directly in the PR concerning this change in Symfony 2.8.

答案是我正在做的模式不再可行,所以我看到了 2 个解决我的问题的方法:

And the answer is that the pattern I was doing is not possible anymore, so I see 2 solutions to my problem:

  • 为自定义类型创建尽可能多的类,而不是一直使用同一个类,并使这些类扩展为抽象类(在我的示例中:创建 Gender1TypeGender2Type 类扩展了 AbstractGenderType 抽象类)
  • 只保留一个类,但添加选项以传递我的特定参数.
  • Create as many classes as I had services for my custom types instead of using all the time the same class, and make these classes extend an abstract one (in my example: create Gender1Type and Gender2Type classes that extend a AbstractGenderType abstract class)
  • Keep only one class, but add options to it to pass my specific parameters.

这篇关于Symfony 2.8/3.0 升级:如何处理带有可变参数的表单类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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