You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Form.php
+86-19Lines changed: 86 additions & 19 deletions
Original file line number
Diff line number
Diff line change
@@ -119,6 +119,26 @@ class Form implements \IteratorAggregate, FormInterface
119
119
*/
120
120
private$synchronized = true;
121
121
122
+
/**
123
+
* Whether the form's data has been initialized.
124
+
*
125
+
* When the data is initialized with its default value, that default value
126
+
* is passed through the transformer chain in order to synchronize the
127
+
* model, normalized and view format for the first time. This is done
128
+
* lazily in order to save performance when {@link setData()} is called
129
+
* manually, making the initialization with the configured default value
130
+
* superfluous.
131
+
*
132
+
* @var Boolean
133
+
*/
134
+
private$initialized = false;
135
+
136
+
/**
137
+
* Whether setData() is currently being called.
138
+
* @var Boolean
139
+
*/
140
+
private$lockSetData = false;
141
+
122
142
/**
123
143
* Creates a new form based on the given configuration.
124
144
*
@@ -138,8 +158,6 @@ public function __construct(FormConfigInterface $config)
138
158
}
139
159
140
160
$this->config = $config;
141
-
142
-
$this->setData($config->getData());
143
161
}
144
162
145
163
publicfunction__clone()
@@ -327,13 +345,16 @@ public function getAttribute($name)
327
345
/**
328
346
* Updates the form with default data.
329
347
*
330
-
* @param array $modelData The data formatted as expected for the underlying object
348
+
* @param mixed $modelData The data formatted as expected for the underlying object
331
349
*
332
350
* @return Form The current form
333
351
*/
334
352
publicfunctionsetData($modelData)
335
353
{
336
-
if ($this->bound) {
354
+
// If the form is bound while disabled, it is set to bound, but the data is not
355
+
// changed. In such cases (i.e. when the form is not initialized yet) don't
356
+
// abort this method.
357
+
if ($this->bound && $this->initialized) {
337
358
thrownewAlreadyBoundException('You cannot change the data of a bound form');
338
359
}
339
360
@@ -346,6 +367,12 @@ public function setData($modelData)
346
367
$modelData = clone$modelData;
347
368
}
348
369
370
+
if ($this->lockSetData) {
371
+
thrownewFormException('A cycle was detected. Listeners to the PRE_SET_DATA event must not call setData(). You should call setData() on the FormEvent object instead.');
0 commit comments