Symfony2 和 Symfony2注释和选择验证:如何存储和检索选择选项?
我有一个要在表单中显示的动态选项列表,我只想将它们维护在一个地方,因为这些不是我必须关心的唯一数据集。
为了通过 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();
你如何解决这个问题?
参考文献:
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:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
至于在实体本身内部进行验证,不太确定您需要这样做。 form->isValid() 将会检查以确保您的选择有效。但我可以理解 Assert\Choices 应该能够指定服务的期望。
从这里: http://symfony.com/doc/current/reference/constraints/回调.html
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