8000 [Form] Fixed option support in Form component · webmozart/symfony@b733045 · GitHub
[go: up one dir, main page]

Skip to content

Commit b733045

Browse files
committed
[Form] Fixed option support in Form component
1 parent 70d49c3 commit b733045

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+1249
-231
lines changed

CHANGELOG-2.1.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,6 @@ To get the diff between two versions, go to https://github.com/symfony/symfony/c
256256
* forms now don't create an empty object anymore if they are completely
257257
empty and not required. The empty value for such forms is null.
258258
* added constant Guess::VERY_HIGH_CONFIDENCE
259-
* FormType::getDefaultOptions() now sees default options defined by parent types
260259
* [BC BREAK] FormType::getParent() does not see default options anymore
261260
* [BC BREAK] The methods `add`, `remove`, `setParent`, `bind` and `setData`
262261
in class Form now throw an exception if the form is already bound
@@ -266,6 +265,8 @@ To get the diff between two versions, go to https://github.com/symfony/symfony/c
266265
"single_text" unless "with_seconds" is set to true
267266
* checkboxes of in an expanded multiple-choice field don't include the choice
268267
in their name anymore. Their names terminate with "[]" now.
268+
* [BC BREAK] FormType::getDefaultOptions() and FormType::getAllowedOptionValues()
269+
don't receive an options array anymore.
269270

270271
### HttpFoundation
271272

UPGRADE-2.1.md

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@
282282
```
283283
284284
* The options passed to the `getParent()` method of form types no longer
285-
contain default options.
285+
contain default options. They only contain the options passed by the user.
286286
287287
You should check if options exist before attempting to read their value.
288288
@@ -303,6 +303,42 @@
303303
return isset($options['widget']) && 'single_text' === $options['widget'] ? 'text' : 'choice';
304304
}
305305
```
306+
307+
* The methods `getDefaultOptions()` and `getAllowedOptionValues()` of form
308+
types no longer receive an option array.
309+
310+
You can specify options that depend on other options using closures instead.
311+
312+
Before:
313+
314+
```
315+
public function getDefaultOptions(array $options)
316+
{
317+
$defaultOptions = array();
318+
319+
if ($options['multiple']) {
320+
$defaultOptions['empty_data'] = array();
321+
}
322+
323+
return $defaultOptions;
324+
}
325+
```
326+
327+
After:
328+
329+
```
330+
public function getDefaultOptions()
331+
{
332+
$return array(
333+
'empty_data' => function (Options $options, $previousValue) {
334+
return $options['multiple'] ? array() : $previousValue;
335+
}
336+
);
337+
}
338+
```
339+
340+
The second argument `$previousValue` does not have to be specified if not
341+
needed.
306342
307343
* The `add()`, `remove()`, `setParent()`, `bind()` and `setData()` methods in
308344
the Form class now throw an exception if the form is already bound.

src/Symfony/Bridge/Doctrine/Form/Type/DoctrineType.php

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Symfony\Bridge\Doctrine\Form\EventListener\MergeDoctrineCollectionListener;
2020
use Symfony\Bridge\Doctrine\Form\DataTransformer\CollectionToArrayTransformer;
2121
use Symfony\Component\Form\AbstractType;
22+
use Symfony\Component\Form\Options;
2223

2324
abstract class DoctrineType extends AbstractType
2425
{
@@ -42,37 +43,43 @@ public function buildForm(FormBuilder $builder, array $options)
4243
}
4344
}
4445

45-
public function getDefaultOptions(array $options)
46+
public function getDefaultOptions()
4647
{
47-
$defaultOptions = array(
48-
'em' => null,
49-
'class' => null,
50-
'property' => null,
51-
'query_builder' => null,
52-
'loader' => null,
53-
'group_by' => null,
54-
);
48+
$registry = $this->registry;
49+
$type = $this;
5550

56-
$options = array_replace($defaultOptions, $options);
51+
$loader = function (Options $options) use ($type, $registry) {
52+
if (null !== $options['query_builder']) {
53+
$manager = $registry->getManager($options['em']);
5754

58-
if (!isset($options['choice_list'])) {
59-
$manager = $this->registry->getManager($options['em']);
60-
61-
if (isset($options['query_builder'])) {
62-
$options['loader'] = $this->getLoader($manager, $options);
55+
return $type->getLoader($manager, $options['query_builder'], $options['class']);
6356
}
6457

65-
$defaultOptions['choice_list'] = new EntityChoiceList(
58+
return null;
59+
};
60+
61+
$choiceList = function (Options $options) use ($registry) {
62+
$manager = $registry->getManager($options['em']);
63+
64+
return new EntityChoiceList(
6665
$manager,
6766
$options['class'],
6867
$options['property'],
6968
$options['loader'],
7069
$options['choices'],
7170
$options['group_by']
7271
);
73-
}
72+
};
7473

75-
return $defaultOptions;
74+
return array(
75+
'em' => null,
76+
'class' => null,
77+
'property' => null,
78+
'query_builder' => null,
79+
'loader' => $loader,
80+
'choice_list' => $choiceList,
81+
'group_by' => null,
82+
);
7683
}
7784

7885
/**
@@ -82,7 +89,7 @@ public function getDefaultOptions(array $options)
8289
* @param array $options
8390
* @return EntityLoaderInterface
8491
*/
85-
abstract protected function getLoader(ObjectManager $manager, array $options);
92+
abstract public function getLoader(ObjectManager $manager, $queryBuilder, $class);
8693

8794
public function getParent(array $options)
8895
{

src/Symfony/Bridge/Doctrine/Form/Type/EntityType.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ class EntityType extends DoctrineType
2323
* @param array $options
2424
* @return ORMQueryBuilderLoader
2525
*/
26-
protected function getLoader(ObjectManager $manager, array $options)
26+
public function getLoader(ObjectManager $manager, $queryBuilder, $class)
2727
{
2828
return new ORMQueryBuilderLoader(
29-
$options['query_builder'],
29+
$queryBuilder,
3030
$manager,
31-
$options['class']
31+
$class
3232
);
3333
}
3434

src/Symfony/Bridge/Propel1/Form/Type/ModelType.php

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Bridge\Propel1\Form\ChoiceList\ModelChoiceList;
1515
use Symfony\Bridge\Propel1\Form\DataTransformer\CollectionToArrayTransformer;
1616
use Symfony\Component\Form\AbstractType;
17+
use Symfony\Component\Form\Options;
1718
use Symfony\Component\Form\FormBuilder;
1819

1920
/**
@@ -30,8 +31,18 @@ public function buildForm(FormBuilder $builder, array $options)
3031
}
3132
}
3233

33-
public function getDefaultOptions(array $options)
34+
public function getDefaultOptions()
3435
{
36+
$choiceList = function (Options $options) {
37+
return new ModelChoiceList(
38+
$options['class'],
39+
$options['property'],
40+
$options['choices'],
41+
$options['query'],
42+
$options['group_by']
43+
);
44+
};
45+
3546
$defaultOptions = array(
3647
'template' => 'choice',
3748
'multiple' => false,
@@ -40,23 +51,10 @@ public function getDefaultOptions(array $options)
4051
'property' => null,
4152
'query' => null,
4253
'choices' => null,
54+
'choice_list' => $choiceList,
4355
'group_by' => null,
4456
'by_reference' => false,
4557
);
46-
47-
$options = array_replace($defaultOptions, $options);
48-
49-
if (!isset($options['choice_list'])) {
50-
$defaultOptions['choice_list'] = new ModelChoiceList(
51-
$options['class'],
52-
$options['property'],
53-
$options['choices'],
54-
$options['query'],
55-
$options['group_by']
56-
);
57-
}
58-
59-
return $defaultOptions;
6058
}
6159

6260
public function getParent(array $options)

src/Symfony/Bundle/SecurityBundle/Tests/Functional/Bundle/CsrfFormLoginBundle/Form/UserLoginFormType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public function buildForm(FormBuilder $builder, array $options)
7676
/**
7777
* @see Symfony\Component\Form\AbstractType::getDefaultOptions()
7878
*/
79-
public function getDefaultOptions(array $options)
79+
public function getDefaultOptions()
8080
{
8181
/* Note: the form's intention must correspond to that for the form login
8282
* listener in order for the CSRF token to validate successfully.

src/Symfony/Component/Form/AbstractType.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public function createBuilder($name, FormFactoryInterface $factory, array $optio
9696
*
9797
* @return array The default options
9898
*/
99-
public function getDefaultOptions(array $options)
99+
public function getDefaultOptions()
100100
{
101101
return array();
102102
}
@@ -108,7 +108,7 @@ public function getDefaultOptions(array $options)
108108
*
109109
* @return array The allowed option values
110110
*/
111-
public function getAllowedOptionValues(array $options)
111+
public function getAllowedOptionValues()
112112
{
113113
return array();
114114
}

src/Symfony/Component/Form/AbstractTypeExtension.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function buildViewBottomUp(FormView $view, FormInterface $form)
6565
*
6666
* @return array
6767
*/
68-
public function getDefaultOptions(array $options)
68+
public function getDefaultOptions()
6969
{
7070
return array();
7171
}
@@ -77,7 +77,7 @@ public function getDefaultOptions(array $options)
7777
*
7878
* @return array The allowed option values
7979
*/
80-
public function getAllowedOptionValues(array $options)
80+
public function getAllowedOptionValues()
8181
{
8282
return array();
8383
}

0 commit comments

Comments
 (0)
0