Commit ffdbc83
committed
feature symfony#59618 [OptionsResolver] Deprecate defining nested options via
This PR was merged into the 7.3 branch.
Discussion
----------
[OptionsResolver] Deprecate defining nested options via `setDefault()` use `setOptions()` instead
| Q | A
| ------------- | ---
| Branch? | 7.3
| Bug fix? | no
| New feature? | yes
| Deprecations? | yes
| Issues | -
| License | MIT
this removes unnecessary limitations that I hadn't considered when introducing nested options feature in symfony#27291.
#### 1. Allow defining default values for nested options
Imagine you want to define the following nested option in a generic form type:
```php
class GenericType extends AbstractType
{
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefault('foo', function (OptionsResolver $foo) {
$foo->define('bar')->allowedTypes('int');
});
}
}
```
then, I'd like to define a concrete type with a default value for it.
```php
class ConcreteType extends AbstractType
{
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefault('foo', ['bar' => 23]);
}
public function getParent(): string
{
return GenericType::class;
}
}
```
this might seem unexpected, but the fact is that the nested definition for `foo` option in `ConcreteType` is gone. As a result, when resolved, the `foo` option will have a default value (`['bar' => 23]`) but without any constraints, allowing end users any value/type to be passed through this option for `ConcreteType` instances
For example, passing `['foo' => false]` as options for `ConcreteType` would be allowed, instead of requiring an array where `bar` expects an integer value.
#### 2. Allow defining lazy default for nested options
the same problem would occur with a lazy default for a nested definition:
```php
class ConcreteType extends AbstractType
{
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setRequired(['baz'])
$resolver->setDefault('foo', function (Options $options) {
return ['bar' => $options['baz']];
});
}
public function getParent(): string
{
return GenericType::class;
}
}
```
the issue here is the same as in the previous example, meaning this new default essentially overrides/removes the original nested definition
---
the two features mentioned earlier are now supported by introducing a new method `setOptions()`, which separates the nested definition from its default value (whether direct or lazy). Additionally this PR deprecates the practice of defining nested options using `setDefault()` method
this also enables the ability to set default values for prototyped options, which wasn't possible before. For example:
```php
class NavigatorType extends AbstractType
{
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->define('buttons')
->options(function (OptionsResolver $buttons) {
$buttons->setPrototype(true);
$buttons->define('name')->required()->allowedTypes('string');
$buttons->define('type')->default(SubmitType::class)->allowedTypes('string');
$buttons->define('options')->default([])->allowedTypes('array');
})
->default([
'back' => ['name' => 'back', 'options' => ['validate' => false, 'validation_groups' => false]],
'next' => ['name' => 'next'],
'submit' => ['name' => 'submit'],
]);
}
}
```
cheers!
Commits
-------
b0bb9a1 Add setOptions methodsetDefault() use setOptions() instead (yceruto)File tree
11 files changed
+852
-99
lines changed- src/Symfony/Component
- Ldap
- Adapter/ExtLdap
- OptionsResolver
- Debug
- Tests
- Debug
- RateLimiter
11 files changed
+852
-99
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
94 | 94 | | |
95 | 95 | | |
96 | 96 | | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
97 | 116 | | |
98 | 117 | | |
99 | 118 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
170 | 170 | | |
171 | 171 | | |
172 | 172 | | |
173 | | - | |
| 173 | + | |
174 | 174 | | |
175 | 175 | | |
176 | 176 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
21 | | - | |
| 21 | + | |
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
27 | 27 | | |
28 | | - | |
29 | 28 | | |
30 | 29 | | |
31 | 30 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| 8 | + | |
| 9 | + | |
8 | 10 | | |
9 | 11 | | |
10 | 12 | | |
| |||
Lines changed: 10 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
101 | 101 | | |
102 | 102 | | |
103 | 103 | | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
104 | 114 | | |
Lines changed: 14 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
143 | 143 | | |
144 | 144 | | |
145 | 145 | | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
146 | 160 | | |
Lines changed: 66 additions & 42 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
64 | 64 | | |
65 | 65 | | |
66 | 66 | | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
67 | 74 | | |
68 | 75 | | |
69 | 76 | | |
| |||
178 | 185 | | |
179 | 186 | | |
180 | 187 | | |
181 | | - | |
182 | | - | |
183 | | - | |
184 | | - | |
185 | | - | |
186 | | - | |
187 | | - | |
188 | | - | |
189 | | - | |
190 | | - | |
191 | | - | |
192 | | - | |
193 | | - | |
194 | | - | |
195 | 188 | | |
196 | 189 | | |
197 | 190 | | |
| |||
226 | 219 | | |
227 | 220 | | |
228 | 221 | | |
229 | | - | |
230 | | - | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
231 | 229 | | |
232 | 230 | | |
233 | 231 | | |
234 | 232 | | |
| 233 | + | |
235 | 234 | | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
236 | 238 | | |
237 | 239 | | |
238 | 240 | | |
| |||
245 | 247 | | |
246 | 248 | | |
247 | 249 | | |
248 | | - | |
249 | | - | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
250 | 257 | | |
251 | 258 | | |
252 | 259 | | |
| |||
403 | 410 | | |
404 | 411 | | |
405 | 412 | | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
406 | 437 | | |
407 | 438 | | |
408 | 439 | | |
| |||
947 | 978 | | |
948 | 979 | | |
949 | 980 | | |
| 981 | + | |
| 982 | + | |
| 983 | + | |
| 984 | + | |
| 985 | + | |
| 986 | + | |
| 987 | + | |
| 988 | + | |
| 989 | + | |
| 990 | + | |
| 991 | + | |
| 992 | + | |
| 993 | + | |
| 994 | + | |
| 995 | + | |
| 996 | + | |
| 997 | + | |
950 | 998 | | |
951 | 999 | | |
952 | 1000 | | |
| |||
958 | 1006 | | |
959 | 1007 | | |
960 | 1008 | | |
961 | | - | |
962 | 1009 | | |
963 | 1010 | | |
964 | 1011 | | |
| |||
989 | 1036 | | |
990 | 1037 | | |
991 | 1038 | | |
992 | | - | |
993 | | - | |
994 | | - | |
995 | | - | |
996 | | - | |
997 | | - | |
998 | | - | |
999 | | - | |
1000 | | - | |
1001 | | - | |
1002 | | - | |
1003 | | - | |
1004 | | - | |
1005 | | - | |
1006 | | - | |
1007 | | - | |
1008 | | - | |
1009 | | - | |
1010 | | - | |
1011 | | - | |
1012 | | - | |
1013 | | - | |
1014 | | - | |
1015 | 1039 | | |
1016 | 1040 | | |
1017 | 1041 | | |
| |||
Lines changed: 9 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
263 | 263 | | |
264 | 264 | | |
265 | 265 | | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
266 | 275 | | |
0 commit comments