本文共 5855 字,大约阅读时间需要 19 分钟。
Before jumping right into dynamic form generation, let's have a quick review of what a bare form class looks like:
在直接进入动态表单生成之前,让我们先快速回顾一下常规的表单类,如下所示:If this particular section of code isn't already familiar to you, you probably need to take a step back and first review the before proceeding. 如果您不清楚上述这段代码段的话,您可能需要在继续之前先退一步看看 |
Let's assume for a moment that this form utilizes an imaginary "Product" class that has only two relevant properties ("name" and "price"). The form generated from this class will look the exact same regardless of a new Product is being created or if an existing product is being edited (e.g. a product fetched from the database).
现在让我们假定该表单使用一个虚拟的“Product”类,该类仅有两个相关属性("name"和"price")。无论是创建一个新的Product,还是编辑一个已存在的Product(如从数据库是提取一个产品),该类所生成的表单看起来完全相同。Suppose now, that you don't want the user to be able to change the name
value once the object has been created. To do this, you can rely on Symfony's system to analyze the data on the object and modify the form based on the Product object's data. In this entry, you'll learn how to add this level of flexibility to your forms.
So, instead of directly adding that "name" widget via our ProductType form class, let's delegate the responsibility of creating that particular field to an Event Subscriber:
因此,替代直接通过我们的ProductType表单类添加"name"小部件,我们可以创建一个特殊的表单域,并委托它负责事件订阅:
The event subscriber is passed the FormFactory object in its constructor so that our new subscriber is capable of creating the form widget once it is notified of the dispatched event during form creation.
事件订阅器将FormFactory对象传递到它的构造函数中,这样一旦在表单创建期间有事件的调度通知,我们新的订阅器就能够创建表单小部件。
The goal is to create a "name" field only if the underlying Product object is new (e.g. hasn't been persisted to the database). Based on that, the subscriber might look like the following:
我们的目标是只有在Product是新建对象(如:尚未持久化到数据库)时,才创建一个"name"表单域。有鉴于此,订阅器看起来如下所示:It is easy to misunderstand the purpose of the if (null === $data) segment of this event subscriber. To fully understand its role, you might consider also taking a look at the and paying special attention to where setData() is called at the end of the constructor, as well as the setData() method itself. 在本事件订阅器中的if (null === $data)代码段的目的很容易引起误解。要完全认识它的作用,您也许需要考虑看看 ,并特别注意where setData() is called at the end of the constructor以及setData()函数本身。 |
The FormEvents::PRE_SET_DATA
line actually resolves to the string form.pre_set_data
. The serves an organizational purpose. It is a centralized location in which you can find all of the various form events available.
FormEvents::PRE_SET_DATA行
实际上被解析成字符串form.pre_set_data。起到了一个组织的作用,它是一个集中的位置,在这里您可以找到所有可用的各种表单事件。
While this example could have used the form.set_data
event or even the form.post_set_data
events just as effectively, by using form.pre_set_data
we guarantee that the data being retrieved from the Event
object has in no way been modified by any other subscribers or listeners. This is because form.pre_set_data
passes a object instead of the object passed by the form.set_data
event. , unlike its child , lacks a setData() method.
You may view the full list of form events via the , found in the form bundle. 您可以通过
|
转载地址:http://clzhl.baihongyu.com/