1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 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 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478
|
UPGRADE FROM 2.0 to 2.1
=======================
### General
* The merging strategy for `assets_base_urls` and `base_urls` has changed.
Unlike most configuration blocks, successive values for `assets_base_urls`
will overwrite each other instead of being merged. This behavior was chosen
because developers will typically define base URL's for each environment.
Given that most projects tend to inherit configurations (e.g.
`config_test.yml` imports `config_dev.yml`) and/or share a common base
configuration (i.e. `config.yml`), merging could yield a set of base URL's
for multiple environments.
* The priorities for the built-in listeners have changed.
```
2.0 2.1
security.firewall kernel.request 64 8
locale listener kernel.request 0 16
router listener early_request 255 n/a
request 0 32
```
### Doctrine
* The DoctrineBundle is moved from the Symfony repository to the Doctrine repository.
Therefore you should change the namespace of this bundle in your AppKernel.php:
Before: `new Symfony\Bundle\DoctrineBundle\DoctrineBundle()`
After: `new Doctrine\Bundle\DoctrineBundle\DoctrineBundle()`
### HttpFoundation
* Locale management was moved from the Session class to the Request class.
##### Configuring the default locale
Before:
```yaml
framework:
session:
default_locale: fr
```
After:
```yaml
framework:
default_locale: fr
```
##### Retrieving the locale from a Twig template
Before: `{{ app.request.session.locale }}` or `{{ app.session.locale }}`
After: `{{ app.request.locale }}`
##### Retrieving the locale from a PHP template
Before: `$view['session']->getLocale()`
After: `$view['request']->getLocale()`
##### Retrieving the locale from PHP code
Before: `$session->getLocale()`
After: `$request->getLocale()`
##### Simulate old behavior
You can simulate that the locale for the user is still stored in the session by
registering a listener that looks like the following if the parameter which
handles the locale value in the request is `_locale`:
```php
namespace XXX;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class LocaleListener implements EventSubscriberInterface
{
private $defaultLocale;
public function __construct($defaultLocale = 'en')
{
$this->defaultLocale = $defaultLocale;
}
public function onKernelRequest(GetResponseEvent $event)
{
$request = $event->getRequest();
if (!$request->hasPreviousSession()) {
return;
}
if ($locale = $request->attributes->get('_locale')) {
$request->getSession()->set('_locale', $locale);
} else {
$request->setLocale($request->getSession()->get('_locale', $this->defaultLocale));
}
}
static public function getSubscribedEvents()
{
return array(
// must be registered before the default Locale listener
KernelEvents::REQUEST => array(array('onKernelRequest', 17)),
);
}
}
```
* The methods `getPathInfo()`, `getBaseUrl()` and `getBasePath()` of
a `Request` now all return a raw value (vs a urldecoded value before). Any call
to one of these methods must be checked and wrapped in a `rawurldecode()` if
needed.
### Security
* `Symfony\Component\Security\Core\User\UserInterface::equals()` has moved to
`Symfony\Component\Security\Core\User\EquatableInterface::isEqualTo()`.
You must rename the `equals()` method in your implementation of the `User`
class to `isEqualTo()` and implement `EquatableInterface`. Apart from that,
no other changes are required.
Alternatively, you may use the default implementation provided by
`AbstractToken::hasUserChanged()` if you have no need of custom comparison
logic. In this case, do not implement `EquatableInterface` and remove your
comparison method.
Before:
```php
class User implements UserInterface
{
// ...
public function equals(UserInterface $user) { /* ... */ }
// ...
}
```
After:
```php
class User implements UserInterface, EquatableInterface
{
// ...
public function isEqualTo(UserInterface $user) { /* ... */ }
// ...
}
```
* The custom factories for the firewall configuration are now
registered during the build method of bundles instead of being registered
by the end-user. This means that you will need to remove the 'factories'
keys in your security configuration.
Before:
```yaml
security:
factories:
- "%kernel.root_dir%/../src/Acme/DemoBundle/Resources/config/security_factories.yml"
```
```yaml
# src/Acme/DemoBundle/Resources/config/security_factories.yml
services:
security.authentication.factory.custom:
class: Acme\DemoBundle\DependencyInjection\Security\Factory\CustomFactory
tags:
- { name: security.listener.factory }
```
After:
```php
namespace Acme\DemoBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Acme\DemoBundle\DependencyInjection\Security\Factory\CustomFactory;
class AcmeDemoBundle extends Bundle
{
public function build(ContainerBuilder $container)
{
parent::build($container);
$extension = $container->getExtension('security');
$extension->addSecurityListenerFactory(new CustomFactory());
}
}
```
* The Firewall listener is now registered after the Router listener. This
means that specific Firewall URLs (like /login_check and /logout) must now
have proper routes defined in your routing configuration. Also, if you have
a custom 404 error page, make sure that you do not use any security related
features such as `is_granted` on it.
* The user provider configuration has been refactored. The configuration
for the chain provider and the memory provider has been changed:
Before:
```yaml
security:
providers:
my_chain_provider:
providers: [my_memory_provider, my_doctrine_provider]
my_memory_provider:
users:
toto: { password: foobar, roles: [ROLE_USER] }
foo: { password: bar, roles: [ROLE_USER, ROLE_ADMIN] }
```
After:
```yaml
security:
providers:
my_chain_provider:
chain:
providers: [my_memory_provider, my_doctrine_provider]
my_memory_provider:
memory:
users:
toto: { password: foobar, roles: [ROLE_USER] }
foo: { password: bar, roles: [ROLE_USER, ROLE_ADMIN] }
```
* `MutableAclInterface::setParentAcl` now accepts `null`, review any
implementations of this interface to reflect this change.
* The `UserPassword` constraint has moved from the Security Bundle to the Security Component:
Before:
```php
use Symfony\Bundle\SecurityBundle\Validator\Constraint\UserPassword;
use Symfony\Bundle\SecurityBundle\Validator\Constraint as SecurityAssert;
```
After:
```php
use Symfony\Component\Security\Core\Validator\Constraint\UserPassword;
use Symfony\Component\Security\Core\Validator\Constraint as SecurityAssert;
```
### Form
#### BC Breaks in Form Types and Options
* A third argument `$options` was added to the methods `buildView()` and
`buildViewBottomUp()` in `FormTypeInterface` and `FormTypeExtensionInterface`.
Furthermore, `buildViewBottomUp()` was renamed to `finishView()`. At last,
all methods in these types now receive instances of `FormBuilderInterface`
where they received instances of `FormBuilder` before. You need to change the
method signatures in your form types and extensions as shown below.
Before:
```php
use Symfony\Component\Form\FormBuilder;
public function buildForm(FormBuilder $builder, array $options)
```
After:
```php
use Symfony\Component\Form\FormBuilderInterface;
public function buildForm(FormBuilderInterface $builder, array $options)
```
* The method `createBuilder` was removed from `FormTypeInterface` for performance
reasons. It is now not possible anymore to use custom implementations of
`FormBuilderInterface` for specific form types.
If you are in such a situation, you can implement a custom `ResolvedFormTypeInterface`
where you create your own `FormBuilderInterface` implementation. You also need to
register a custom `ResolvedFormTypeFactoryInterface` implementation under the service
name "form.resolved_type_factory" in order to replace the default implementation.
* If you previously inherited from `FieldType`, you should now inherit from
`FormType`. You should also set the option `compound` to `false` if your field
is not supposed to contain child fields.
`FieldType` was deprecated and will be removed in Symfony 2.3.
Before:
```php
public function getParent(array $options)
{
return 'field';
}
```
After:
```php
public function getParent()
{
return 'form';
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'compound' => false,
));
}
```
The changed signature of `getParent()` is explained in the next step.
The new method `setDefaultOptions` is described in the section "Deprecations".
* No options are passed to `getParent()` of `FormTypeInterface` anymore. If
you previously dynamically inherited from `FormType` or `FieldType`, you can now
dynamically set the "compound" option instead.
Before:
```php
public function getParent(array $options)
{
return $options['expanded'] ? 'form' : 'field';
}
```
After:
```php
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\OptionsResolver\Options;
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$compound = function (Options $options) {
return $options['expanded'];
};
$resolver->setDefaults(array(
'compound' => $compound,
));
}
public function getParent()
{
return 'form';
}
```
The new method `setDefaultOptions` is described in the section "Deprecations".
* The "data_class" option now *must* be set if a form maps to an object. If
you leave it empty, the form will expect an array, an instance of \ArrayAccess
or a scalar value and fail with a corresponding exception.
Likewise, if a form maps to an array or an instance of \ArrayAccess, the option
*must* be left null now.
Form mapped to an instance of `Person`:
```php
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Acme\Demo\Person',
));
}
```
* The mapping of property paths to arrays has changed.
Previously, a property path "street" mapped to both a field `$street` of
a class (or its accessors `getStreet()` and `setStreet()`) and an index
`['street']` of an array or an object implementing `\ArrayAccess`.
Now, the property path "street" only maps to a class field (or accessors),
while the property path "[street]" only maps to indices.
If you defined property paths manually in the "property_path" option, you
should revise them and adjust them if necessary.
Before:
```php
$builder->add('name', 'text', array(
'property_path' => 'address.street',
));
```
After (if the address object is an array):
```php
$builder->add('name', 'text', array(
'property_path' => 'address[street]',
));
```
If address is an object in this case, the code given in "Before"
works without changes.
* Form and field names must now start with a letter, digit or underscore
and only contain letters, digits, underscores, hyphens and colons.
* In the collection type's template, the default name of the prototype field
has changed from `$$name$$` to `__name__`.
You can now customize the name of the prototype field by changin the
"prototype_name" option. You are advised to prepend and append two
underscores wherever you specify a value for the field's "prototype_name"
option.
```php
$builder->add('tags', 'collection', array('prototype_name' => '__proto__'));
// results in the name "__proto__" in the template
```
* The "read_only" option now renders as `readonly="readonly"`, use
"disabled" instead for `disabled="disabled"`.
* Child forms are no longer automatically validated. That means that you must
explicitly set the `Valid` constraint in your model if you want to validate
objects modified by child forms.
If you don't want to set the `Valid` constraint, or if there is no reference
from the data of the parent form to the data of the child form, you can
enable BC behavior by setting the "cascade_validation" option to `true`
on the parent form.
#### BC Breaks in Themes and HTML
* FormType and FieldType were merged and require you to adapt your form
themes.
The block `field_widget` and all references to it should be renamed to
`form_widget_simple`:
Before:
```jinja
{% block url_widget %}
{% spaceless %}
{% set type = type|default('url') %}
{{ block('field_widget') }}
{% endspaceless %}
{% endblock url_widget %}
```
After:
```jinja
{% block url_widget %}
{% spaceless %}
{% set type = type|default('url') %}
{{ block('form_widget_simple') }}
{% endspaceless %}
{% endblock url_widget %}
```
All other `field_*` blocks and references to them should be renamed to
`form_*`. If you previously defined both a `field_*` and a `form_*`
block, you can merge them into a single `form_*` block and check the new
Boolean variable `compound` instead:
Before:
```jinja
{% block form_errors %}
{% spaceless %}
... form code ...
{% endspaceless %}
{% endblock form_errors %}
{% block field_errors %}
{% spaceless %}
... field code ...
{% endspaceless %}
{% endblock field_errors %}
```
After:
```jinja
{% block form_errors %}
{% spaceless %}
{% if compound %}
... form code ...
{% else %}
... field code ...
{% endif %}
{% endspaceless %}
{% endblock form_errors %}
```
Furthermore, the block `generic_label` was merged into `form_label`. You
should now override `form_label` in order to customize labels.
Last but not least, the block `widget_choice_options` was renamed to
`choice_widget_options` to be consistent with the rest of the default
theme.
* The strategy for generating the `id` and `name` HTML attributes for
checkboxes and radio buttons in a choice field has changed.
Instead of appending the choice value, a generated integer is now appended
by default. Take care if your JavaScript relies on that. If you want to
read the actual choice value, read the `value` attribute instead.
* In the choice field type's template, the `_form_is_choice_selected` method
used to identify a selected choice has been replaced with the `selectedchoice`
filter. Similarly, the `_form_is_choice_group` method used to check if a
choice is grouped has been removed and can be checked with the `iterable`
test.
Before:
```jinja
{% for choice, label in choices %}
{% if _form_is_choice_group(label) %}
<optgroup label="{{ choice|trans }}">
{% for nestedChoice, nestedLabel in label %}
... options tags ...
{% endfor %}
</optgroup>
{% else %}
<option value="{{ choice }}"{% if _form_is_choice_selected(form, choice) %} selected="selected"{% endif %}>
{{ label }}
</option>
{% endif %}
{% endfor %}
```
After:
```jinja
{% for label, choice in choices %}
{% if choice is iterable %}
<optgroup label="{{ label|trans({}, translation_domain) }}">
{% for nestedChoice, nestedLabel in choice %}
... options tags ...
{% endfor %}
</optgroup>
{% else %}
<option value="{{ choice.value }}"{% if choice is selectedchoice(value) %} selected="selected"{% endif %}>
{{ label }}
</option>
{% endif %}
{% endfor %}
```
* Creation of default labels has been moved to the view layer. You will need
to incorporate this logic into any custom `form_label` templates to
accommodate those cases when the `label` option has not been explicitly
set.
```jinja
{% block form_label %}
{% if label is empty %}
{% set label = name|humanize %}
{% endif %}
{# ... #}
{% endblock %}
````
* Custom styling of individual rows of a collection form has been removed for
performance reasons. Instead, all rows now have the same block name, where
the word "entry" replaces the previous occurrence of the row index.
Before:
```jinja
{% block _author_tags_0_label %}
{# ... #}
{% endblock %}
{% block _author_tags_1_label %}
{# ... #}
{% endblock %}
```
After:
```jinja
{% block _author_tags_entry_label %}
{# ... #}
{% endblock %}
```
* The method `renderBlock()` of the helper for the PHP Templating component was
renamed to `block()`. Its first argument is now expected to be a `FormView`
instance.
Before:
```php
<?php echo $view['form']->renderBlock('widget_attributes') ?>
```
After:
```php
<?php echo $view['form']->block($form, 'widget_attributes') ?>
```
#### Other BC Breaks
* The order of the first two arguments of the methods `createNamed` and
`createNamedBuilder` in `FormFactoryInterface` was reversed to be
consistent with the rest of the component. You should scan your code
for occurrences of these methods and reverse the parameters.
Before:
```php
$form = $factory->createNamed('text', 'firstName');
```
After:
```php
$form = $factory->createNamed('firstName', 'text');
```
* The implementation of `ChoiceList` was changed heavily. As a result,
`ArrayChoiceList` was replaced. If you have custom classes that extend
this class, you must now extend `SimpleChoiceList` and pass choices
to the parent constructor.
Before:
```php
class MyChoiceList extends ArrayChoiceList
{
protected function load()
{
parent::load();
// load choices
$this->choices = $choices;
}
}
```
After:
```php
class MyChoiceList extends SimpleChoiceList
{
public function __construct()
{
// load choices
parent::__construct($choices);
}
}
```
If you need to load the choices lazily -- that is, as soon as they are
accessed for the first time -- you can extend `LazyChoiceList` instead
and load the choices by overriding `loadChoiceList()`.
```php
class MyChoiceList extends LazyChoiceList
{
protected function loadChoiceList()
{
// load choices
return new SimpleChoiceList($choices);
}
}
```
`PaddedChoiceList`, `MonthChoiceList` and `TimezoneChoiceList` were removed.
Their functionality was merged into `DateType`, `TimeType` and `TimezoneType`.
`EntityChoiceList` was adapted. The methods `getEntities()`,
`getEntitiesByKeys()`, `getIdentifier()` and `getIdentifierValues()` were
removed or made private. Instead of the first two, you can now use
`getChoices()` and `getChoicesByValues()`. For the latter two, no
replacement exists.
* HTML attributes are now passed in the `label_attr` variable for the `form_label` function.
Before:
```jinja
{{ form_label(form.name, 'Your Name', { 'attr': {'class': 'foo'} }) }}
```
After:
```jinja
{{ form_label(form.name, 'Your Name', { 'label_attr': {'class': 'foo'} }) }}
```
* `EntitiesToArrayTransformer` and `EntityToIdTransformer` were removed.
The former was replaced by `CollectionToArrayTransformer` in combination
with `EntityChoiceList`, the latter is not required in the core anymore.
* The following transformers were renamed:
* `ArrayToBooleanChoicesTransformer` to `ChoicesToBooleanArrayTransformer`
* `ScalarToBooleanChoicesTransformer` to `ChoiceToBooleanArrayTransformer`
* `ArrayToChoicesTransformer` to `ChoicesToValuesTransformer`
* `ScalarToChoiceTransformer` to `ChoiceToValueTransformer`
to be consistent with the naming in `ChoiceListInterface`.
* `FormUtil::toArrayKey()` and `FormUtil::toArrayKeys()` were removed.
They were merged into ChoiceList and have no public equivalent anymore.
* The `add()`, `remove()`, `setParent()`, `bind()` and `setData()` methods in
the Form class now throw an exception if the form is already bound.
If you used these methods on bound forms, you should consider moving your
logic to an event listener that observes `FormEvents::PRE_BIND` or
`FormEvents::BIND`.
#### Deprecations
* The following methods of `FormTypeInterface` and `FormTypeExtensionInterface`
are deprecated and will be removed in Symfony 2.3:
* `getDefaultOptions`
* `getAllowedOptionValues`
You should use the newly added `setDefaultOptions` instead, which gives you
access to the OptionsResolverInterface instance and with that a lot more power.
Before:
```php
public function getDefaultOptions(array $options)
{
return array(
'gender' => 'male',
);
}
public function getAllowedOptionValues(array $options)
{
return array(
'gender' => array('male', 'female'),
);
}
```
After:
```php
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'gender' => 'male',
));
$resolver->setAllowedValues(array(
'gender' => array('male', 'female'),
));
}
```
You can specify options that depend on other options using closures.
Before:
```php
public function getDefaultOptions(array $options)
{
$defaultOptions = array();
if ($options['multiple']) {
$defaultOptions['empty_data'] = array();
}
return $defaultOptions;
}
```
After:
```php
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'empty_data' => function (Options $options, $value) {
return $options['multiple'] ? array() : $value;
}
));
}
```
The second argument `$value` contains the current default value and
does not have to be specified if not needed.
* The following methods in `FormBuilder` were deprecated and have a new
equivalent:
* `prependClientTransformer`: `addViewTransformer`, with `true` as second argument
* `appendClientTransformer`: `addViewTransformer`
* `getClientTransformers`: `getViewTransformers`
* `resetClientTransformers`: `resetViewTransformers`
* `prependNormTransformer`: `addModelTransformer`
* `appendNormTransformer`: `addModelTransformer`, with `true` as second argument
* `getNormTransformers`: `getModelTransformers`
* `resetNormTransformers`: `resetModelTransformers`
The deprecated methods will be removed in Symfony 2.3. You are advised to
update your application.
Before:
```php
$builder->appendClientTransformer(new MyTransformer());
```
After:
```php
$builder->addViewTransformer(new MyTransformer());
```
* The following events were deprecated and have a new equivalent:
* `FormEvents::SET_DATA`: `FormEvents::PRE_SET_DATA`
* `FormEvents::BIND_CLIENT_DATA`: `FormEvents::PRE_BIND`
* `FormEvents::BIND_NORM_DATA`: `FormEvents::BIND`
The deprecated events will be removed in Symfony 2.3.
Furthermore, the event classes `DataEvent` and `FilterDataEvent` were
deprecated and replaced by the generic `FormEvent`. You are advised to
code your listeners against the new event now. The deprecated events will
be removed in Symfony 2.3.
Before:
```php
$builder->addListener(FormEvents::BIND_CLIENT_DATA, function (FilterDataEvent $event) {
// ...
});
```
After:
```php
$builder->addListener(FormEvents::PRE_BIND, function (FormEvent $event) {
// ...
});
```
* The interface `FormValidatorInterface` was deprecated and will be removed
in Symfony 2.3.
If you implemented custom validators using this interface, you can
substitute them by event listeners listening to the `FormEvents::POST_BIND`
(or any other of the `*BIND` events). In case you used the CallbackValidator
class, you should now pass the callback directly to `addEventListener`.
* The method `guessMinLength()` of `FormTypeGuesserInterface` was deprecated
and will be removed in Symfony 2.3. You should use the new method
`guessPattern()` instead which may return any regular expression that
is inserted in the HTML5 attribute `pattern`.
Before:
```php
public function guessMinLength($class, $property)
{
if (/* condition */) {
return new ValueGuess($minLength, Guess::LOW_CONFIDENCE);
}
}
```
After:
```php
public function guessPattern($class, $property)
{
if (/* condition */) {
return new ValueGuess('.{'.$minLength.',}', Guess::LOW_CONFIDENCE);
}
}
```
* Setting the option "property_path" to `false` was deprecated and will be unsupported
as of Symfony 2.3.
You should use the new option "mapped" instead in order to set that you don't want
a field to be mapped to its parent's data.
Before:
```php
$builder->add('termsAccepted', 'checkbox', array(
'property_path' => false,
));
```
After:
```php
$builder->add('termsAccepted', 'checkbox', array(
'mapped' => false,
));
```
* The following methods in `Form` were deprecated and will be removed in
Symfony 2.3:
* `getTypes`
* `getErrorBubbling`
* `getNormTransformers`
* `getClientTransformers`
* `getAttribute`
* `hasAttribute`
* `getClientData`
* `getChildren`
* `hasChildren`
* `bindRequest`
Before:
```php
$form->getErrorBubbling()
```
After:
```php
$form->getConfig()->getErrorBubbling();
```
The method `getClientData` has a new equivalent that is named `getViewData`.
You can access all other methods on the `FormConfigInterface` object instead.
Instead of `getChildren` and `hasChildren`, you should now use `all` and
`count`.
Before:
```php
if ($form->hasChildren()) {
```
After:
```php
if (count($form) > 0) {
```
Instead of `bindRequest`, you should now simply call `bind`:
Before:
```php
$form->bindRequest($request);
```
After:
```php
$form->bind($request);
```
* The option "validation_constraint" was deprecated and will be removed
in Symfony 2.3. You should use the option "constraints" instead,
where you can pass one or more constraints for a form.
Before:
```php
$builder->add('name', 'text', array(
'validation_constraint' => new NotBlank(),
));
```
After:
```php
$builder->add('name', 'text', array(
'constraints' => new NotBlank(),
));
```
Unlike previously, you can also pass a list of constraints now:
```php
$builder->add('name', 'text', array(
'constraints' => array(
new NotBlank(),
new MinLength(3),
),
));
```
Be aware that constraints will now only be validated if they belong
to the validated group! So if you validate a form in group "Custom"
and previously did:
```php
$builder->add('name', 'text', array(
'validation_constraint' => new NotBlank(),
));
```
Then you need to add the constraint to the group "Custom" now:
```php
$builder->add('name', 'text', array(
'constraints' => new NotBlank(array('groups' => 'Custom')),
));
```
* The options "data_timezone" and "user_timezone" in `DateType`,
`DateTimeType` and `TimeType` were deprecated and will be removed in
Symfony 2.3. They were renamed to "model_timezone" and "view_timezone".
Before:
```php
$builder->add('scheduledFor', 'date', array(
'data_timezone' => 'UTC',
'user_timezone' => 'America/New_York',
));
```
After:
```php
$builder->add('scheduledFor', 'date', array(
'model_timezone' => 'UTC',
'view_timezone' => 'America/New_York',
));
```
* The methods `addType`, `hasType` and `getType` in `FormFactory` are deprecated
and will be removed in Symfony 2.3. You should use the methods with the same
name on the `FormRegistry` instead.
Before:
```php
$this->get('form.factory')->addType(new MyFormType());
```
After:
```php
$registry = $this->get('form.registry');
$registry->addType($registry->resolveType(new MyFormType()));
```
* The following methods in class `FormView` were deprecated and will be
removed in Symfony 2.3:
* `set`
* `has`
* `get`
* `all`
* `getVars`
* `addChild`
* `getChild`
* `getChildren`
* `removeChild`
* `hasChild`
* `hasChildren`
* `getParent`
* `hasParent`
* `setParent`
You should access the public properties `vars`, `children` and `parent`
instead.
Before:
```php
$view->set('help', 'A text longer than six characters');
$view->set('error_class', 'max_length_error');
```
After:
```php
$view->vars = array_replace($view->vars, array(
'help' => 'A text longer than six characters',
'error_class' => 'max_length_error',
));
```
Before:
```php
echo $view->get('error_class');
```
After:
```php
echo $view->vars['error_class'];
```
Before:
```php
if ($view->hasChildren()) { ...
```
After:
```php
if (count($view->children)) { ...
```
### Validator
* The methods `setMessage()`, `getMessageTemplate()` and
`getMessageParameters()` in the `ConstraintValidator` class were deprecated and will
be removed in Symfony 2.3.
If you have implemented custom validators, you should use the
`addViolation()` method on the `ExecutionContext` object instead.
Before:
```php
public function isValid($value, Constraint $constraint)
{
// ...
if (!$valid) {
$this->setMessage($constraint->message, array(
'{{ value }}' => $value,
));
return false;
}
}
```
After:
```php
public function isValid($value, Constraint $constraint)
{
// ...
if (!$valid) {
$this->context->addViolation($constraint->message, array(
'{{ value }}' => $value,
));
return false;
}
}
```
* The method `setPropertyPath()` in the ExecutionContext class
was removed.
You should use the `addViolationAtSubPath()` method on the
`ExecutionContext` object instead.
Before:
```php
public function isPropertyValid(ExecutionContext $context)
{
// ...
$propertyPath = $context->getPropertyPath().'.property';
$context->setPropertyPath($propertyPath);
$context->addViolation('Error Message', array(), null);
}
```
After:
```php
public function isPropertyValid(ExecutionContext $context)
{
// ...
$context->addViolationAtSubPath('property', 'Error Message', array(), null);
}
```
* The method `isValid` of `ConstraintValidatorInterface` was renamed to
`validate` and its return value was dropped.
`ConstraintValidator` still contains the deprecated `isValid` method and
forwards `validate` calls to `isValid` by default. This BC layer will be
removed in Symfony 2.3. You are advised to rename your methods. You should
also remove the return values, which have never been used by the framework.
Before:
```php
public function isValid($value, Constraint $constraint)
{
// ...
if (!$valid) {
$this->context->addViolation($constraint->message, array(
'{{ value }}' => $value,
));
return false;
}
}
```
After:
```php
public function validate($value, Constraint $constraint)
{
// ...
if (!$valid) {
$this->context->addViolation($constraint->message, array(
'{{ value }}' => $value,
));
return;
}
}
```
* Core translation messages changed. A dot is added at the end of each message.
Overwritten core translations need to be fixed.
* Collections (arrays or instances of `\Traversable`) in properties
annotated with `Valid` are not traversed recursively by default anymore.
This means that if a collection contains an entry which is again a
collection, the inner collection won't be traversed anymore as it
happened before. You can set the BC behavior by setting the new property
`deep` of `Valid` to `true`.
Before:
```php
/** @Assert\Valid */
private $recursiveCollection;
```
After:
```php
/** @Assert\Valid(deep = true) */
private $recursiveCollection;
```
* The `Size`, `Min` and `Max` constraints were deprecated and will be removed in
Symfony 2.3. You should use the new constraint `Range` instead.
Before:
```php
/** @Assert\Size(min = 2, max = 16) */
private $numberOfCpus;
```
After:
```php
/** @Assert\Range(min = 2, max = 16) */
private $numberOfCpus;
```
Before:
```php
/** @Assert\Min(2) */
private $numberOfCpus;
```
After:
```php
/** @Assert\Range(min = 2) */
private $numberOfCpus;
```
* The `MinLength` and `MaxLength` constraints were deprecated and will be
removed in Symfony 2.3. You should use the new constraint `Length` instead.
Before:
```php
/** @Assert\MinLength(8) */
private $password;
```
After:
```php
/** @Assert\Length(min = 8) */
private $password;
```
* The classes `ValidatorContext` and `ValidatorFactory` were deprecated and
will be removed in Symfony 2.3. You should use the new entry point
`Validation` instead.
Before:
```php
$validator = ValidatorFactory::buildDefault(array('path/to/mapping.xml'))
->getValidator();
```
After:
```php
$validator = Validation::createValidatorBuilder()
->addXmlMapping('path/to/mapping.xml')
->getValidator();
```
### Session
* The namespace of the Session class changed from `Symfony\Component\HttpFoundation\Session`
to `Symfony\Component\HttpFoundation\Session\Session`.
* Using `get` to retrieve flash messages now returns an array.
##### Retrieving the flash messages from a Twig template
Before:
```jinja
{% if app.session.hasFlash('notice') %}
<div class="flash-notice">
{{ app.session.getFlash('notice') }}
</div>
{% endif %}
```
After:
```jinja
{% for flashMessage in app.session.flashbag.get('notice') %}
<div class="flash-notice">
{{ flashMessage }}
</div>
{% endfor %}
```
You can process all flash messages in a single loop with:
```jinja
{% for type, flashMessages in app.session.flashbag.all() %}
{% for flashMessage in flashMessages %}
<div class="flash-{{ type }}">
{{ flashMessage }}
</div>
{% endfor %}
{% endfor %}
```
* Session handler drivers should implement `\SessionHandlerInterface` or extend from
`Symfony\Component\HttpFoundation\Session\Storage\Handler\NativeHandlerInterface` base class and renamed
to `Handler\FooSessionHandler`. E.g. `PdoSessionStorage` becomes `Handler\PdoSessionHandler`.
* Refactor code using `$session->*flash*()` methods to use `$session->getFlashBag()->*()`.
### Serializer
* The key names created by the `GetSetMethodNormalizer` have changed from
all lowercased to camelCased (e.g. `mypropertyvalue` to `myPropertyValue`).
* The `item` element is now converted to an array when deserializing XML.
```xml
<?xml version="1.0"?>
<response>
<item><title><![CDATA[title1]]></title></item><item><title><![CDATA[title2]]></title></item>
</response>
```
Before:
Array()
After:
Array(
[item] => Array(
[0] => Array(
[title] => title1
)
[1] => Array(
[title] => title2
)
)
)
### Routing
* The UrlMatcher urldecodes the route parameters only once, they were
decoded twice before. Note that the `urldecode()` calls have been changed for a
single `rawurldecode()` in order to support `+` for input paths.
* Two new parameters have been added to the DIC: `router.request_context.host`
and `router.request_context.scheme`. You can customize them for your
functional tests or for generating urls with the right host and scheme
when your are in the cli context.
### FrameworkBundle
* session options: lifetime, path, domain, secure, httponly were deprecated.
Prefixed versions should now be used instead: cookie_lifetime, cookie_path, cookie_domain, cookie_secure, cookie_httponly
Before:
```yaml
framework:
session:
lifetime: 3600
path: \
domain: example.com
secure: true
httponly: true
```
After:
```yaml
framework:
session:
cookie_lifetime: 3600
cookie_path: \
cookie_domain: example.com
cookie_secure: true
cookie_httponly: true
```
Added `handler_id`, defaults to `session.handler.native_file`.
```yaml
framework:
session:
storage_id: session.storage.native
handler_id: session.handler.native_file
```
To use mock session storage use the following. `handler_id` is irrelevant in this context.
```yaml
framework:
session:
storage_id: session.storage.mock_file
```
### WebProfilerBundle
* You must clear old profiles after upgrading to 2.1. If you are using a
database then you will need to remove the table.
|