Symfony2 和 Symfony2注释和选择验证:如何存储和检索选择选项?

发布于 2025-01-03 13:17:45 字数 1930 浏览 3 评论 0原文

我有一个要在表单中显示的动态选项列表,我只想将它们维护在一个地方,因为这些不是我必须关心的唯一数据集。

为了通过 Choices-CallbackValidator 验证实体中的这些选择,我需要指定一个修复类和函数,其中将返回选择。

这似乎不对,因为我仍在学习 Symfony2 和 DIC 概念,然后我不会期望指定具体的回调函数/类,而是指定一个服务或考虑另一种方法来解决这个问题。

我不想提供类名,而是提供服务名称作为回调。我这里错了吗?

的选项列表

  • 我需要填写表单
  • ,以使用注释验证实体
  • ,以了解向何处发送电子邮件(稍后)

立即设置:

services.yml - 使用 service_container 定义的 DataManager 和作为服务的表单(我不知道是否这是正确的):

services:
  data_manager:
    class:  TestBundle\Service\DataManager
    arguments:
            - "@service_container"
            - %tc_data.list%
  support_type_form:
    class: TestBundle\Form\Type\TicketType
    arguments: ["@service_container"]
    tags:
        - { name: form.type }

表单:

class TicketType extends FormType
{
    public function buildForm(FormBuilder $builder, array $options)
    {
        $theChoices = $this->getContainer()->get('data_manager')->getTheChoices();
        ...
    }
}

实体:

class Ticket
{
    /**
     * @Assert\NotBlank()
     * @Assert\Choice(callback = {"NotAServiceReference", "getTheChoices"})
     */
    private $the_list_field;
}

所以我有服务中的数据列表,但是您建议我如何检索选项来验证实体?

我想到的另一个解决方案是使用回调验证(链接2),但即使在文档中它也说:

// somehow you have an array of "fake names"
$fakeNames = array();

你如何解决这个问题?

参考文献:

  1. 请参阅:http ://symfony.com/doc/current/reference/constraints/Choice.html#supplying-the-choices-with-a-callback-function(点击注释)
  2. 请参阅:http://symfony.com/doc/current /reference/constraints/Callback.html#the-callback-method

I have a dynamic list of choices to display in a form and I just want to maintain them in one place, as these are not the only datasets I have to care for.

To validate those choices in the Entity over choices-CallbackValidator I would need to specify a fix class and function where the choices would be returned.

This seems not right as I am still learning Symfony2 and the DIC concept and then I would not expect to specify a concrete callback function/class, but a service or think of another way to solve this.

I would like to give not a classname, but a service name as callback. Am I wrong here?

I need the list of options to

  • fill the form
  • to validate the entity with annotations
  • to know where to send emails (later)

Setup now:

services.yml - The DataManager defined with service_container and the Form as a service (I don't know if this is right):

services:
  data_manager:
    class:  TestBundle\Service\DataManager
    arguments:
            - "@service_container"
            - %tc_data.list%
  support_type_form:
    class: TestBundle\Form\Type\TicketType
    arguments: ["@service_container"]
    tags:
        - { name: form.type }

The Form:

class TicketType extends FormType
{
    public function buildForm(FormBuilder $builder, array $options)
    {
        $theChoices = $this->getContainer()->get('data_manager')->getTheChoices();
        ...
    }
}

The Entity:

class Ticket
{
    /**
     * @Assert\NotBlank()
     * @Assert\Choice(callback = {"NotAServiceReference", "getTheChoices"})
     */
    private $the_list_field;
}

So I have the list of data in the service, but how would you suggest I retrieve the choices to validate the Entity?

Another solution I thought of was using the Callback validation (link 2), but even there in the docs it sais:

// somehow you have an array of "fake names"
$fakeNames = array();

How did/would you solve this?

References:

  1. See: http://symfony.com/doc/current/reference/constraints/Choice.html#supplying-the-choices-with-a-callback-function (click annotations)
  2. See: http://symfony.com/doc/current/reference/constraints/Callback.html#the-callback-method

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

  1. 避免将 service_container 传递给您的服务。依赖注入背后的基本思想是仅注入那些实际需要的对象。
  2. 因此,对于您的 DataManager,您可能只想注入实体管理器。
  3. 对于您的 TicketType,注入您的 DataManager 服务。此时您可以以良好的通用方式执行 $this->dataManager->getChoices() 。

至于在实体本身内部进行验证,不太确定您需要这样做。 form->isValid() 将会检查以确保您的选择有效。但我可以理解 Assert\Choices 应该能够指定服务的期望。

从这里: http://symfony.com/doc/current/reference/constraints/回调.html

但是,目前无法将服务指定为约束。要使用服务进行验证,您应该创建一个自定义验证约束并将该新约束添加到您的类中。

  1. Avoid passing service_container to your services. The basic idea behind dependency injection is to inject only those objects that are actually needed.
  2. So for your DataManager you probably only want to inject the entity manager.
  3. For your TicketType, inject your DataManager service. At which point you can do $this->dataManager->getChoices() in a nice generic fashion.

As far as validating within the entity itself, not really sure you need to. The form->isValid() will already check to make sure your choices are valid. But I can understand the expectation that Assert\Choices should be able to specify a service.

From here: http://symfony.com/doc/current/reference/constraints/Callback.html

It is not currently possible, however, to specify a service as a constraint. To validate using a service, you should create a custom validation constraint and add that new constraint to your class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文