Sonataadminbundle: sonata_type_model causes cyclic calls in normalizers

Created on 5 Apr 2017  路  12Comments  路  Source: sonata-project/SonataAdminBundle

Environment

Sonata packages

sonata-project/admin-bundle              3.16.0 The missing Symfony Admin Generator
sonata-project/block-bundle              3.3.2  Symfony SonataBlockBundle
sonata-project/cache                     1.0.7  Cache library
sonata-project/core-bundle               3.3.0  Symfony SonataCoreBundle
sonata-project/datagrid-bundle           2.2.1  Symfony SonataDatagridBundle
sonata-project/doctrine-orm-admin-bundle 3.1.4  Symfony Sonata / Integrate Doctrine ORM into the SonataAdminBundle
sonata-project/exporter                  1.7.1  Lightweight Exporter library

Symfony packages

symfony/monolog-bundle     v2.12.1 Symfony MonologBundle
symfony/phpunit-bridge     v2.8.19 Symfony PHPUnit Bridge
symfony/polyfill-apcu      v1.3.0  Symfony polyfill backporting apcu_* functions to lower PHP versions
symfony/polyfill-intl-icu  v1.3.0  Symfony polyfill for intl's ICU-related data and classes
symfony/polyfill-mbstring  v1.3.0  Symfony polyfill for the Mbstring extension
symfony/polyfill-php54     v1.3.0  Symfony polyfill backporting some PHP 5.4+ features to lower PHP versions
symfony/polyfill-php55     v1.3.0  Symfony polyfill backporting some PHP 5.5+ features to lower PHP versions
symfony/polyfill-php56     v1.3.0  Symfony polyfill backporting some PHP 5.6+ features to lower PHP versions
symfony/polyfill-php70     v1.3.0  Symfony polyfill backporting some PHP 7.0+ features to lower PHP versions
symfony/polyfill-util      v1.3.0  Symfony utilities for portability of PHP codes
symfony/security-acl       v3.0.0  Symfony Security Component - ACL (Access Control List)
symfony/swiftmailer-bundle v2.5.4  Symfony SwiftmailerBundle
symfony/symfony            v2.8.19 The Symfony PHP framework

PHP version

PHP 5.6.17 (cli) (built: Jan  6 2016 13:28:21)
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2015 Zend Technologies

Subject

In symfony 2.8.19 this commit has been merged: https://github.com/symfony/symfony/commit/fb4d8de5e4c99d074d6b8f67f9251671014a931b This will trigger the choice_loader normalizer if "choices_as_values" is not set (default in ChoiceType) or set to false explicit.

The $choiceListNormalizer in Symfonys ChoiceType accesses the "choices" option which will trigger the $choicesNormalizer which will trigger the $choicesAsValuesNormalizer (because the choices_as_values check is false) which will then access the "choice_loader" but Sonatas ModelChoiceLoader also accesses the "choices" which will cause a cyclic access.

This can easily get fixed by setting the "choices_as_values" option explicit to true. The ModelType should set this option internally to avoid the cyclic call and it also fixes to display the deprecation message.

Steps to reproduce

Create a form with "sonata_type_model" type.

Expected results

Form is displayed.

Actual results

The options "choice_list", "choice_loader", "choices", "choices_as_values" have a cyclic dependency.
500 Internal Server Error - OptionDefinitionException

`
Stack Trace

in vendor\symfony\symfony\src\Symfony\Component\OptionsResolver\OptionsResolver.php at line 846
at OptionsResolver ->offsetGet ('choice_loader')
in vendor\symfony\symfony\src\Symfony\Component\Form\Extension\Core\Type\ChoiceType.php at line 360
at ChoiceType ->Symfony\Component\Form\Extension\Core\Type\{closure} (object(OptionsResolver), false)
in vendor\symfony\symfony\src\Symfony\Component\OptionsResolver\OptionsResolver.php at line 965
at OptionsResolver ->offsetGet ('choices_as_values')
in vendor\symfony\symfony\src\Symfony\Component\Form\Extension\Core\Type\ChoiceType.php at line 290
at ChoiceType ->Symfony\Component\Form\Extension\Core\Type\{closure} (object(OptionsResolver), array())
in vendor\symfony\symfony\src\Symfony\Component\OptionsResolver\OptionsResolver.php at line 965
at OptionsResolver ->offsetGet ('choices')
in app\cache\dev\classes.php at line 7614
at ModelType ->Sonata\AdminBundle\Form\Type\{closure} (object(OptionsResolver), null)
in vendor\symfony\symfony\src\Symfony\Component\OptionsResolver\OptionsResolver.php at line 859
at OptionsResolver ->offsetGet ('choice_loader')
in vendor\symfony\symfony\src\Symfony\Component\Form\Extension\Core\Type\ChoiceType.php at line 341
at ChoiceType ->Symfony\Component\Form\Extension\Core\Type\{closure} (object(OptionsResolver), null)
in vendor\symfony\symfony\src\Symfony\Component\OptionsResolver\OptionsResolver.php at line 965
at OptionsResolver ->offsetGet ('choice_list')
in vendor\symfony\symfony\src\Symfony\Component\OptionsResolver\OptionsResolver.php at line 791
at OptionsResolver ->resolve (array('sonata_field_description' => object(FieldDescription), 'class' => 'AppBundle\Entity\TestEntity', 'model_manager' => object(ModelManager), 'required' => false, 'multiple' => true, 'btn_add' => false, 'property_path' => 'genres', 'label_render' => false, 'label' => 'Test Entity'))
in vendor\symfony\symfony\src\Symfony\Component\Form\ResolvedFormType.php at line 156

`

bug critical vendor

Most helpful comment

@gremo, yes it is solve the problem. Today i downgrade my project to symfony 2.8.18 and everything good now.

All 12 comments

Thanks @eschultz-magix ! Could you please provide a PR ?

I see this issue on SonataUserBundle when creating/editing a user. Have to revert to SF 2.8.18 until this gets fixed.

Sorry @greg0ire, unfortunately my company doesn't offer that time for me. If anyone else could do a PR for me that would be very welcome.

Instead of just setting 'choices_as_values' to true as default value in ModelType, also a normalizer should check whether a developer has set this flag explicit to false. A deprecation notice should be thrown then to indicate that this option has no impact to the ModelChoiceLoader nor ModelChoiceList and the normalizer has to reset the option to true.

@jordisala1991 You can set the "choices_as_values" option to true manually for each field using the ModelType. That was the quick fix I did in my code:

$formMapper
  ->add('genres', 'sonata_type_model', array(
    'choices_as_values' => true,
    'required' => false,
    'multiple' => true,
    'btn_add' => false,
))

Anyone can confirm that downgrading to Symfony 2.8.18 solve the problem?

@gremo, yes it is solve the problem. Today i downgrade my project to symfony 2.8.18 and everything good now.

Can any of you make a PR that solves the issue, or that simply prevents people from upgrading to 2.8.19 ?

@eschultz-magix suggestion about adding 'choices_as_values' => true option indeed fixes the issue.

Maybe we should add this default option on the ModelType?

@Soullivaneuh Great, but setting 'choices_as_values' => true as default option in ModelType doesn't prevent to override this value with false. Therefore a normalizer has to ensure that the choices_as_values option is always set to true internally.

In my case, adding 'choices_as_values' => true option in the type options solves the problem

->add('categories', 'sonata_type_model', array(
'choices_as_values' => true,
'required' => false,
'expanded' => true,
'multiple' => true,
))

I'm getting the same error now :/

symfony/asset                                    v3.0.9             Symfony Asset Component
symfony/assetic-bundle                           v2.8.1             Integrates Assetic into Symfony2
symfony/cache                                    v3.3.2             Symfony Cache component with PSR-6, PSR-16, and tags
symfony/class-loader                             v2.8.22            Symfony ClassLoader Component
symfony/config                                   v2.8.22            Symfony Config Component
symfony/console                                  v2.8.22            Symfony Console Component
symfony/css-selector                             v3.3.2             Symfony CssSelector Component
symfony/debug                                    v2.8.22            Symfony Debug Component
symfony/debug-bundle                             v3.1.10            Symfony DebugBundle
symfony/dependency-injection                     v2.8.22            Symfony DependencyInjection Component
symfony/doctrine-bridge                          v3.2.9             Symfony Doctrine Bridge
symfony/dom-crawler                              v3.3.2             Symfony DomCrawler Component
symfony/event-dispatcher                         v3.0.9             Symfony EventDispatcher Component
symfony/expression-language                      v3.3.2             Symfony ExpressionLanguage Component
symfony/filesystem                               v3.0.9             Symfony Filesystem Component
symfony/finder                                   v2.8.22            Symfony Finder Component
symfony/form                                     v2.8.22            Symfony Form Component
symfony/framework-bundle                         v2.8.22            Symfony FrameworkBundle
symfony/http-foundation                          v2.8.22            Symfony HttpFoundation Component
symfony/http-kernel                              v2.8.22            Symfony HttpKernel Component
symfony/intl                                     v3.2.9             A PHP replacement layer for the C intl extension that includes additional data from the ICU library.
symfony/monolog-bridge                           v3.2.9             Symfony Monolog Bridge
symfony/monolog-bundle                           v3.1.0             Symfony MonologBundle
symfony/options-resolver                         v2.8.22            Symfony OptionsResolver Component
symfony/phpunit-bridge                           v3.3.2             Symfony PHPUnit Bridge
symfony/polyfill-apcu                            v1.4.0             Symfony polyfill backporting apcu_* functions to lower PHP versions
symfony/polyfill-intl-icu                        v1.4.0             Symfony polyfill for intl's ICU-related data and classes
symfony/polyfill-mbstring                        v1.4.0             Symfony polyfill for the Mbstring extension
symfony/polyfill-php54                           v1.4.0             Symfony polyfill backporting some PHP 5.4+ features to lower PHP versions
symfony/polyfill-php55                           v1.4.0             Symfony polyfill backporting some PHP 5.5+ features to lower PHP versions
symfony/polyfill-php56                           v1.4.0             Symfony polyfill backporting some PHP 5.6+ features to lower PHP versions
symfony/polyfill-php70                           v1.4.0             Symfony polyfill backporting some PHP 7.0+ features to lower PHP versions
symfony/polyfill-util                            v1.4.0             Symfony utilities for portability of PHP codes
symfony/process                                  v2.8.22            Symfony Process Component
symfony/property-access                          v3.0.9             Symfony PropertyAccess Component
symfony/routing                                  v2.8.22            Symfony Routing Component
symfony/security                                 v2.8.5             Symfony Security Component
symfony/security-acl                             v3.0.0             Symfony Security Component - ACL (Access Control List)
symfony/security-bundle                          v2.8.22            Symfony SecurityBundle
symfony/security-csrf                            v2.8.22            Symfony Security Component - CSRF Library
symfony/stopwatch                                v3.0.9             Symfony Stopwatch Component
symfony/swiftmailer-bundle                       v2.6.2             Symfony SwiftmailerBundle
symfony/templating                               v2.8.22            Symfony Templating Component
symfony/translation                              v2.8.22            Symfony Translation Component
symfony/twig-bridge                              v2.8.22            Symfony Twig Bridge
symfony/twig-bundle                              v3.1.10            Symfony TwigBundle
symfony/validator                                v3.2.9             Symfony Validator Component
symfony/var-dumper                               v3.3.2             Symfony mechanism for exploring and dumping PHP variables
symfony/web-profiler-bundle                      v2.8.22            Symfony WebProfilerBundle
symfony/yaml                                     v3.3.2             Symfony Yaml Component

sonata-project/admin-bundle                      3.19.0             The missing Symfony Admin Generator
sonata-project/block-bundle                      3.3.2              Symfony SonataBlockBundle
sonata-project/cache                             1.0.7              Cache library
sonata-project/cache-bundle                      2.3.1              This bundle provides caching services
sonata-project/classification-bundle             3.3.1              Symfony SonataClassificationBundle
sonata-project/core-bundle                       3.4.0              Symfony SonataCoreBundle
sonata-project/dashboard-bundle                  dev-master 1d62f37 Provides a Dashboard management through container and block services
sonata-project/datagrid-bundle                   2.2.1              Symfony SonataDatagridBundle
sonata-project/doctrine-extensions               1.0.2              Doctrine2 behavioral extensions
sonata-project/doctrine-orm-admin-bundle         3.1.5              Symfony Sonata / Integrate Doctrine ORM into the SonataAdminBundle
sonata-project/easy-extends-bundle               2.2.0              Symfony SonataEasyExtendsBundle
sonata-project/exporter                          1.7.1              Lightweight Exporter library
sonata-project/formatter-bundle                  3.2.2              Symfony SonataFormatterBundle
sonata-project/google-authenticator              2.0.0              Library to integrate Google Authenticator into a PHP project
sonata-project/intl-bundle                       2.3.0              Symfony SonataIntlBundle
sonata-project/media-bundle                      3.5.1              Symfony SonataMediaBundle
sonata-project/news-bundle                       3.1.2              Symfony SonataNewsBundle
sonata-project/notification-bundle               3.1.0              Symfony SonataNotificationBundle
sonata-project/page-bundle                       3.5.0              This bundle provides a Site and Page management through container and block services
sonata-project/seo-bundle                        2.2.0              Symfony SonataSeoBundle
sonata-project/timeline-bundle                   3.1.1              Integrates SpyTimelineBundle into Sonata
sonata-project/user-bundle                       3.2.4              Symfony SonataUserBundle

Can somebody confirm? @greg0ire @OskarStark ?

I'm getting the same, but 'choices_as_values' => true solves it.

Strange bug... Just ran composer update again and the problem looks solved

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kiler129 picture kiler129  路  3Comments

peter-gribanov picture peter-gribanov  路  3Comments

core23 picture core23  路  3Comments

parijathakumar picture parijathakumar  路  3Comments

jenyak picture jenyak  路  3Comments