sonata-project / SonataAdminBundle

The missing Symfony Admin Generator
https://docs.sonata-project.org/projects/SonataAdminBundle
MIT License
2.11k stars 1.26k forks source link

CollectionType inline table, errors are not shown under the form widget anymore? #6166

Closed gremo closed 4 years ago

gremo commented 4 years ago

Environment

Sonata packages

$ composer show --latest 'sonata-project/*'
sonata-project/admin-bundle              3.70.1 3.70.1 The missing Symfony Admin Generator
sonata-project/block-bundle              3.20.0 4.2.0  Symfony SonataBlockBundle
sonata-project/cache                     1.1.1  2.0.1  Cache library
sonata-project/cache-bundle              2.4.2  3.2.1  This bundle provides caching services
sonata-project/classification-bundle     3.12.1 3.12.1 Symfony SonataClassificationBundle
sonata-project/core-bundle               3.20.0 3.20.0 Symfony SonataCoreBundle (abandoned)
sonata-project/datagrid-bundle           2.5.0  3.2.0  Symfony SonataDatagridBundle
sonata-project/doctrine-extensions       1.6.0  1.6.0  Doctrine2 behavioral extensions
sonata-project/doctrine-orm-admin-bundle 3.18.0 3.18.0 Symfony Sonata / Integrate Doctrine ORM into the SonataAdmin...
sonata-project/easy-extends-bundle       2.5.0  2.5.0  Symfony SonataEasyExtendsBundle
sonata-project/exporter                  2.2.0  2.2.0  Lightweight Exporter library
sonata-project/form-extensions           0.1.2  1.4.0  Symfony form extensions
sonata-project/formatter-bundle          4.2.0  4.2.0  Symfony SonataFormatterBundle
sonata-project/intl-bundle               2.7.0  2.7.0  Symfony SonataIntlBundle
sonata-project/media-bundle              3.25.0 3.25.0 Symfony SonataMediaBundle
sonata-project/notification-bundle       3.7.0  3.7.0  Symfony SonataNotificationBundle
sonata-project/page-bundle               3.17.3 3.17.3 This bundle provides a Site and Page management through cont...
sonata-project/seo-bundle                2.10.0 2.10.0 Symfony SonataSeoBundle
sonata-project/twig-extensions           0.1.1  1.3.0  Sonata twig extensions
sonata-project/user-bundle               4.5.3  4.5.3  Symfony SonataUserBundle

Symfony packages

$ composer show --latest 'symfony/*'
symfony/apache-pack                v1.0.1  v1.0.1  A pack for Apache support in Symfony
symfony/asset                      v4.4.10 v4.4.10 Symfony Asset Component
symfony/browser-kit                v4.4.10 v4.4.10 Symfony BrowserKit Component
symfony/cache                      v4.4.10 v4.4.10 Symfony Cache component with PSR-6, PSR-16, and tags
symfony/cache-contracts            v2.1.2  v2.1.2  Generic abstractions related to caching
symfony/config                     v4.4.10 v4.4.10 Symfony Config Component
symfony/console                    v4.4.10 v4.4.10 Symfony Console Component
symfony/css-selector               v4.4.10 v4.4.10 Symfony CssSelector Component
symfony/debug                      v4.4.10 v4.4.10 Symfony Debug Component
symfony/debug-bundle               v4.4.10 v4.4.10 Symfony DebugBundle
symfony/debug-pack                 v1.0.8  v1.0.8  A debug pack for Symfony projects
symfony/dependency-injection       v4.4.10 v4.4.10 Symfony DependencyInjection Component
symfony/deprecation-contracts      v2.1.2  v2.1.2  A generic function and convention to trigger deprecation notices
symfony/doctrine-bridge            v4.4.10 v4.4.10 Symfony Doctrine Bridge
symfony/dom-crawler                v4.4.10 v4.4.10 Symfony DomCrawler Component
symfony/dotenv                     v4.4.10 v4.4.10 Registers environment variables from a .env file
symfony/error-handler              v4.4.10 v4.4.10 Symfony ErrorHandler Component
symfony/event-dispatcher           v4.4.10 v4.4.10 Symfony EventDispatcher Component
symfony/event-dispatcher-contracts v1.1.7  v2.1.2  Generic abstractions related to dispatching event
symfony/expression-language        v4.4.10 v4.4.10 Symfony ExpressionLanguage Component
symfony/filesystem                 v4.4.10 v4.4.10 Symfony Filesystem Component
symfony/finder                     v4.4.10 v4.4.10 Symfony Finder Component
symfony/flex                       v1.8.4  v1.8.4  Composer plugin for Symfony
symfony/form                       v4.4.10 v4.4.10 Symfony Form Component
symfony/framework-bundle           v4.4.10 v4.4.10 Symfony FrameworkBundle
symfony/http-client                v4.4.10 v4.4.10 Symfony HttpClient component
symfony/http-client-contracts      v2.1.2  v2.1.2  Generic abstractions related to HTTP clients
symfony/http-foundation            v4.4.10 v4.4.10 Symfony HttpFoundation Component
symfony/http-kernel                v4.4.10 v4.4.10 Symfony HttpKernel Component
symfony/inflector                  v4.4.10 v4.4.10 Symfony Inflector Component
symfony/intl                       v4.4.10 v4.4.10 A PHP replacement layer for the C intl extension that includes a...
symfony/mailer                     v4.4.10 v4.4.10 Symfony Mailer Component
symfony/maker-bundle               v1.19.0 v1.19.0 Symfony Maker helps you create empty commands, controllers, form...
symfony/mime                       v4.4.10 v4.4.10 A library to manipulate MIME messages
symfony/monolog-bridge             v4.4.10 v4.4.10 Symfony Monolog Bridge
symfony/monolog-bundle             v3.5.0  v3.5.0  Symfony MonologBundle
symfony/options-resolver           v4.4.10 v4.4.10 Symfony OptionsResolver Component
symfony/orm-pack                   v1.0.8  v1.0.8  A pack for the Doctrine ORM
symfony/phpunit-bridge             v5.1.2  v5.1.2  Symfony PHPUnit Bridge
symfony/polyfill-intl-grapheme     v1.17.1 v1.17.1 Symfony polyfill for intl's grapheme_* functions
symfony/polyfill-intl-icu          v1.17.1 v1.17.1 Symfony polyfill for intl's ICU-related data and classes
symfony/polyfill-intl-idn          v1.17.1 v1.17.1 Symfony polyfill for intl's idn_to_ascii and idn_to_utf8 functions
symfony/polyfill-intl-normalizer   v1.17.1 v1.17.1 Symfony polyfill for intl's Normalizer class and related functions
symfony/polyfill-mbstring          v1.17.1 v1.17.1 Symfony polyfill for the Mbstring extension
symfony/polyfill-php72             v1.17.0 v1.17.0 Symfony polyfill backporting some PHP 7.2+ features to lower PHP...
symfony/polyfill-php73             v1.17.1 v1.17.1 Symfony polyfill backporting some PHP 7.3+ features to lower PHP...
symfony/polyfill-php80             v1.17.1 v1.17.1 Symfony polyfill backporting some PHP 8.0+ features to lower PHP...
symfony/process                    v4.4.10 v4.4.10 Symfony Process Component
symfony/profiler-pack              v1.0.4  v1.0.4  A pack for the Symfony web profiler
symfony/property-access            v4.4.10 v4.4.10 Symfony PropertyAccess Component
symfony/property-info              v4.4.10 v4.4.10 Symfony Property Info Component
symfony/routing                    v4.4.10 v4.4.10 Symfony Routing Component
symfony/security-acl               v3.0.4  v3.0.4  Symfony Security Component - ACL (Access Control List)
symfony/security-bundle            v4.4.10 v4.4.10 Symfony SecurityBundle
symfony/security-core              v4.4.10 v4.4.10 Symfony Security Component - Core Library
symfony/security-csrf              v4.4.10 v4.4.10 Symfony Security Component - CSRF Library
symfony/security-guard             v4.4.10 v4.4.10 Symfony Security Component - Guard
symfony/security-http              v4.4.10 v4.4.10 Symfony Security Component - HTTP Integration
symfony/serializer                 v4.4.10 v4.4.10 Symfony Serializer Component
symfony/serializer-pack            v1.0.3  v1.0.3  A pack for the Symfony serializer
symfony/service-contracts          v2.1.2  v2.1.2  Generic abstractions related to writing services
symfony/stopwatch                  v4.4.10 v4.4.10 Symfony Stopwatch Component
symfony/string                     v5.1.2  v5.1.2  Symfony String component
symfony/swiftmailer-bundle         v3.4.0  v3.4.0  Symfony SwiftmailerBundle
symfony/templating                 v4.4.10 v4.4.10 Symfony Templating Component
symfony/test-pack                  v1.0.6  v1.0.6  A pack for functional and end-to-end testing within a Symfony app
symfony/translation                v4.4.10 v4.4.10 Symfony Translation Component
symfony/translation-contracts      v2.1.2  v2.1.2  Generic abstractions related to translation
symfony/twig-bridge                v4.4.10 v4.4.10 Symfony Twig Bridge
symfony/twig-bundle                v4.4.10 v4.4.10 Symfony TwigBundle
symfony/twig-pack                  v1.0.0  v1.0.0  A Twig pack for Symfony projects
symfony/validator                  v4.4.10 v4.4.10 Symfony Validator Component
symfony/var-dumper                 v4.4.10 v4.4.10 Symfony mechanism for exploring and dumping PHP variables
symfony/var-exporter               v4.4.10 v4.4.10 A blend of var_export() + serialize() to turn any serializable d...
symfony/web-profiler-bundle        v4.4.10 v4.4.10 Symfony WebProfilerBundle
symfony/web-server-bundle          v4.4.10 v4.4.10 Symfony WebServerBundle
symfony/webpack-encore-bundle      v1.7.3  v1.7.3  Integration with your Symfony app & Webpack Encore!
symfony/yaml                       v4.4.10 v4.4.10 Symfony Yaml Component

PHP version

$ php -v
PHP 7.4.7 (cli) (built: Jun  9 2020 13:34:30) ( NTS Visual C++ 2017 x64 )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.7, Copyright (c), by Zend Technologies

Subject

With CollectionType inline table, now errors are bubbled into parent form. Didn't happened in

Steps to reproduce

Create a one-to-may relation for the News entity, named newsHasImages, towards NewsHasImage. Add a form type CollectionType in order to manage it. Create an admin for the NewsHasImage entity and add a required file (for example, a Media field).

Add a new row (inline-table) and save without selecting a media.

Expected results

Error are shown right under the field "media".

Actual results

Errors are bubbled.

Cattura

Hint

The same code works with the following:

"sonata-project/admin-bundle": "3.58.0"
"sonata-project/doctrine-orm-admin-bundle": "3.14"

Introduced with version 3.66.0,

working

VincentLanglet commented 4 years ago

Thanks for the report @gremo. Did you tried others version to help to find the commit with the bug ?

gremo commented 4 years ago

@VincentLanglet I only spotted that the bug was introduced in 3.66.0 (no specific commits).

VincentLanglet commented 4 years ago

@gremo It works correctly for me image

gremo commented 4 years ago

Can you put here a minimal example (entity and admin)? I am pretty sure that it’s Working only downgrading and without the red text like you shown ☹️

VincentLanglet commented 4 years ago

Can you put here a minimal example (entity and admin)?

Create a one-to-may relation for the News entity, named Images, towards Image, and with Assert\Valid. Add a required field with an Assert\NotNull constraint on Image. Add a form type CollectionType in order to manage it. Create an admin for the Image entity.

I am pretty sure that it’s Working only downgrading and without the red text like you shown ☹️

If you create a repository with your issue ; I will be able to debug it.

gremo commented 4 years ago

Ok, I'll try to ugrade again and double check the issue, than I'll create a minimal example. Can you please show yourshow sonata-project/* --latest?

VincentLanglet commented 4 years ago

Sure,

sonata-project/admin-bundle                   3.69.0     3.71.0     The missing Symfony Admin Generator
sonata-project/block-bundle                   3.18.5     4.2.0      Symfony SonataBlockBundle
sonata-project/cache                          2.0.1      2.0.1      Cache library
sonata-project/core-bundle                    3.20.0     3.20.0     Symfony SonataCoreBundle (abandoned)
sonata-project/doctrine-extensions            1.6.0      1.6.0      Doctrine2 behavioral extensions
sonata-project/doctrine-orm-admin-bundle      3.18.0     3.19.0     Symfony Sonata / Integrate Doctrine ORM into the SonataAdminBundle
sonata-project/exporter                       2.2.0      2.2.0      Lightweight Exporter library
sonata-project/form-extensions                0.1.2      1.5.0      Symfony form extensions
sonata-project/twig-extensions                0.1.1      1.3.0      Sonata twig extensions

I'll be happy to try to debug your example :)

gremo commented 4 years ago

Ok, done it, not so "minimal"... but here we are:

git clone https://github.com/gremo/sonata-admin-6166.git
cd sonata-admin-6166/
yarn install
composer install

Then, create your .env.local as usual (DATABASE_URL and optional DATABASE_VERSION). Then

php bin/console doctrine:database:create
php bin/console doctrine:schema:update --force
php bin/console fos:user:create --super-admin admin admin@localhost password
yarn encore dev

Go to http://localhost:8000/admin/app/news/create

Let me know! Thanks!

VincentLanglet commented 4 years ago

The issue was introduced by https://github.com/sonata-project/SonataAdminBundle/pull/6066 Which is improved by https://github.com/sonata-project/SonataAdminBundle/pull/6171 But still doesn't solve.

The main goal of these PR was to set the parent subject when creating a new entity. Before this, $this->getSubject()->getNews() was returning null in the NewsHasImageAdmin. But now it returns the news you're creating/editing. This allow to have a specific form (you can filter some ChoiceType depending on some News values), which can be useful when editing.

This create an issue with the handleRequest. Before when you create and submit a row 0, since the News entity had no NewsHasImage value, it add the row 0. But now, when you submit the row 0, since the row 0 was already set to the News entity, it remove the row 0 and re-add a new row, the row 1 with the same values. This means that the form error which was associated to the field 0 doesn't find the field anymore and is displayed in the top of the page.

I need some more investigations. If you can give me any idea/help ; I take it.

gremo commented 4 years ago

So, that was my fault? Why your example seems working?

The main goal of these PR was to set the parent subject when creating a new entity. Before this, $this->getSubject()->getNews() was returning null in the NewsHasImageAdmin. But now it returns the news you're creating/editing. This allow to have a specific form (you can filter some ChoiceType depending on some News values), which can be useful > > > when editing.

Perfectly clear. yes, I was aware that it was not possible to access the parent subject from a nested admin. I solved the issue some times ago for a project, but it was a dirty workaround, messing with event listeners (if I recall correctly).

I need some more investigations. If you can give me any idea/help ; I take it.

My understandings of Sonata internals are not even close to yours, so I doubt, I'm sorry :(

VincentLanglet commented 4 years ago

So, that was my fault? Why your example seems working?

Its working when you're editing an existant NewsHasImage entity (since the index does not change). But it doesn't work when you're trying to add a new one.

I reproduced the issue on my personnal project.

I need some more investigations. If you can give me any idea/help ; I take it.

My understandings of Sonata internals are not even close to yours, so I doubt, I'm sorry :(

I don't know if it's Sonata or Symfony related. My understanding are not the best too ; I essentially dump data until I find the solution.

Removing this line https://github.com/sonata-project/SonataAdminBundle/blob/3.x/src/Form/Type/AdminType.php#L112 Fix the issue. But we lost the fact the parent subject is set to the new row...

Everything is happening here: https://github.com/sonata-project/SonataAdminBundle/blob/3.x/src/Controller/CRUDController.php#L313-L314

We're setting a News with no NewsHasImage relation. Then we're handle the request with a NewHasImage field submitted. And then the form field has the index 1 instead of 0.

This is maybe related to https://github.com/sonata-project/form-extensions/blob/1.x/src/EventListener/ResizeFormListener.php I saw that this listener is triggered during the handleRequest. But I currently doesn't understand everything in this file.

VincentLanglet commented 4 years ago

@gremo I'm looking for a solution.

I think I have one. Can you try https://github.com/sonata-project/SonataAdminBundle/pull/6171 ? It seems to work for me.

gremo commented 4 years ago

Thanks @VincentLanglet for helping us. I'm quite busy at work right now, but I should test your fix during this weekend.

VincentLanglet commented 4 years ago

@gremo Did you find time to try it ? I'll soon finish this PR.

gremo commented 4 years ago

@VincentLanglet right now. I dirty cloned your branch directly into the vendor folder. Seems working fine, however as I said... I don't know if something else will break, my understanding of internals isn't good:

Cattura

Sidenote: why you see the red error message but I can't? 😄

VincentLanglet commented 4 years ago

Sidenote: why you see the red error message but I can't? 😄

I think I have an old fix in my project. Maybe I can make a PR for Sonata.

gremo commented 4 years ago

You should, It exists since months or years, can't remember... probably never worked 😂

gremo commented 4 years ago

@VincentLanglet thank you.

VincentLanglet commented 4 years ago

You're welcome. I also opened a PR for the design fix https://github.com/sonata-project/SonataAdminBundle/pull/6195