Symfony 2 - 初始化表单对象后修改表单配置 [英] Symfony 2 - modifying form configuration after initializing a form object

查看:131
本文介绍了Symfony 2 - 初始化表单对象后修改表单配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以在表单对象初始化后修改表单配置(我将表单定义为服务)。

即,创建我们可以这样做:

$ form = $ this-> createForm('some_form_type',$ entity,[' some_key'=> true]);



但如果我想修改 $ options
$ b

FormInterface 公开 getConfig()返回 FormConfigInterface 的方法,所以我们可以直接访问配置对象。但是,这个对象不会暴露任何setter。 FormInterface 也不公开任何配置设置方法。



总之,你知道一种修改在初始化表单对象之后创建一个配置对象?

解决方案

我想问你的第一个问题是:在构建表单对象后修改表单配置?



这实际上是不可能的,因为配置在被表单构建器使用之前被锁定( FormBuilderInterface ),以创建和初始化表单( FormInterface )实例。



因此,除非您使用自己的 FormBuilderInterface FormConfigBuilder 一次构建后不锁定配置,您应该在构建表单实例之前使用表单构建器来正确修改任何内容


I was wondering if it's possible to modify form configuration after the form object has been initialized (I define my form as a service).

Namely, when creating a form in a controller, we could do this:

$form = $this->createForm('some_form_type', $entity, ['some_key' => true]);

but then what if I want to modify $options after initialization.

FormInterface exposes getConfig() method that returns FormConfigInterface so we have direct access to the configuration object. This object, however, does not expose any setters. FormInterface also does not expose any configuration setter methods.

In short, do you know of a way to modify a form configuration object after initilizaing the form object?

解决方案

First question I'd like to ask you is: Why would you modify the form configuration after the form object is built ?

That's actually impossible, because the configuration is locked before being used by the form builder (FormBuilderInterface) in order to create and initialize the form (FormInterface) instance.

So, unless you use your own FormBuilderInterface and FormConfigBuilder instances not locking the configuration once build, you should use the form builder to properly modify anything before building the form instance.

这篇关于Symfony 2 - 初始化表单对象后修改表单配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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