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

Error when ModelAutocompleteType is used in nested collection with custom entry_type #5477

Closed watari closed 2 years ago

watari commented 5 years ago

Environment

Sonata packages

$ composer show --latest 'sonata-project/*'
sonata-project/admin-bundle              3.45.1 3.45.2 The missing Symfony Admin Generator
sonata-project/block-bundle              3.14.0 3.14.0 Symfony SonataBlockBundle
sonata-project/cache                     2.0.1  2.0.1  Cache library
sonata-project/core-bundle               3.15.1 3.16.1 Symfony SonataCoreBundle
sonata-project/datagrid-bundle           2.4.0  2.4.0  Symfony SonataDatagridBundle
sonata-project/doctrine-extensions       1.1.5  1.1.5  Doctrine2 behavioral extensions
sonata-project/doctrine-orm-admin-bundle 3.8.2  3.8.2  Symfony Sonata / Integrate Doctrine ORM into the SonataAdminBundle
sonata-project/easy-extends-bundle       2.5.0  2.5.0  Symfony SonataEasyExtendsBundle
sonata-project/exporter                  2.0.1  2.0.1  Lightweight Exporter library
sonata-project/user-bundle               4.3.0  4.3.0  Symfony SonataUserBundle

Symfony packages

$ composer show --latest 'symfony/*'
symfony/acl-bundle           v1.0.0  v1.0.0  Symfony AclBundle
symfony/asset                v4.2.3  v4.2.3  Symfony Asset Component
symfony/cache                v4.2.3  v4.2.3  Symfony Cache component with PSR-6, PSR-16, and tags
symfony/config               v4.2.3  v4.2.3  Symfony Config Component
symfony/console              v4.2.3  v4.2.3  Symfony Console Component
symfony/contracts            v1.0.2  v1.0.2  A set of abstractions extracted out of the Symfony components
symfony/debug                v4.2.3  v4.2.3  Symfony Debug Component
symfony/dependency-injection v4.2.3  v4.2.3  Symfony DependencyInjection Component
symfony/doctrine-bridge      v4.2.3  v4.2.3  Symfony Doctrine Bridge
symfony/dotenv               v4.2.3  v4.2.3  Registers environment variables from a .env file
symfony/event-dispatcher     v4.2.3  v4.2.3  Symfony EventDispatcher Component
symfony/expression-language  v4.2.3  v4.2.3  Symfony ExpressionLanguage Component
symfony/filesystem           v4.2.3  v4.2.3  Symfony Filesystem Component
symfony/finder               v4.2.3  v4.2.3  Symfony Finder Component
symfony/flex                 v1.1.8  v1.1.8  Composer plugin for Symfony
symfony/form                 v4.2.3  v4.2.3  Symfony Form Component
symfony/framework-bundle     v4.2.3  v4.2.3  Symfony FrameworkBundle
symfony/http-foundation      v4.2.3  v4.2.3  Symfony HttpFoundation Component
symfony/http-kernel          v4.2.3  v4.2.3  Symfony HttpKernel Component
symfony/inflector            v4.2.3  v4.2.3  Symfony Inflector Component
symfony/intl                 v4.2.3  v4.2.3  A PHP replacement layer for the C intl extension that includes additional data from the ICU library.
symfony/options-resolver     v4.2.3  v4.2.3  Symfony OptionsResolver Component
symfony/polyfill-intl-icu    v1.10.0 v1.10.0 Symfony polyfill for intl's ICU-related data and classes
symfony/polyfill-mbstring    v1.10.0 v1.10.0 Symfony polyfill for the Mbstring extension
symfony/property-access      v4.2.3  v4.2.3  Symfony PropertyAccess Component
symfony/routing              v4.2.3  v4.2.3  Symfony Routing Component
symfony/security-acl         v3.0.1  v3.0.1  Symfony Security Component - ACL (Access Control List)
symfony/security-bundle      v4.2.3  v4.2.3  Symfony SecurityBundle
symfony/security-core        v4.2.3  v4.2.3  Symfony Security Component - Core Library
symfony/security-csrf        v4.2.3  v4.2.3  Symfony Security Component - CSRF Library
symfony/security-guard       v4.2.3  v4.2.3  Symfony Security Component - Guard
symfony/security-http        v4.2.3  v4.2.3  Symfony Security Component - HTTP Integration
symfony/swiftmailer-bundle   v3.2.5  v3.2.5  Symfony SwiftmailerBundle
symfony/templating           v4.2.3  v4.2.3  Symfony Templating Component
symfony/translation          v4.2.3  v4.2.3  Symfony Translation Component
symfony/twig-bridge          v4.2.3  v4.2.3  Symfony Twig Bridge
symfony/twig-bundle          v4.2.3  v4.2.3  Symfony TwigBundle
symfony/validator            v4.2.3  v4.2.3  Symfony Validator Component
symfony/var-exporter         v4.2.3  v4.2.3  A blend of var_export() + serialize() to turn any serializable data structure to plain PHP code
symfony/yaml                 v4.2.3  v4.2.3  Symfony Yaml Component

PHP version

$ php -v
PHP 7.2.15-1+ubuntu16.04.1+deb.sury.org+1 (cli) (built: Feb  8 2019 15:37:29) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
    with Xdebug v2.6.1, Copyright (c) 2002-2018, by Derick Rethans
    with Zend OPcache v7.2.15-1+ubuntu16.04.1+deb.sury.org+1, Copyright (c) 1999-2018, by Zend Technologie

Subject

Hi there! I getting Twig runtime error "Impossible to access an attribute ("uniqid") on a boolean variable ("")." when try to use ModelAutocompleteType for field in entity that belong to nested collection in form.

Similar problem was previously reported here (https://github.com/sonata-project/SonataAdminBundle/issues/3727). I tried to search for solutions but failed to find any. Maybe I just doing something wrong, but I cant figure it out.

In my case I have Bill and BillLine entities with One-to-Many relation. Also there Type entity to which BillLine referenced with Many-to-One. I tried to create form in which Bill with related bill lines will be created. In this form I wanted to have autocomplete for type field. This field is associated with Type entity. For this purpose I tried to use ModelAutocompleteType. For configuring form block for each BillLine instance I created custom Symfony form type. As options for this form type I passed from Admin class model_manager and reference to parent admin instance.

Steps to reproduce

  1. Create two entity classes with relation One-to-Many.
  2. Create custom type for "nested" entity class. Specify for one of the fields type as ModelAutocompleteType
  3. Create Admin class for "parent" entity and configure form with CollectionType for field with "nested" entities.
  4. Specify as entry_type custom type for "nested" entity class.
  5. Try to render form.

    Expected results

    Form successfully created. When New Collection item is added, auto-complete in item-related block work correctly.

    Actual results

    Twig runtime error "Impossible to access an attribute ("uniqid") on a boolean variable ("")."

watari commented 5 years ago

I think, that problem can be fixed if:

  1. condition will be changed sonata_type_model_autocomplete.html.twig:112 from {% if sonata_admin.admin is not null %} to {% if sonata_admin.admin is not empty %}
  2. in options will be passed admin_code of related to custom form type Admin class.

In my case with this changes problem was fixed. What do you think about this solution? Will it brake something?

jean-gui commented 5 years ago

I have been experiencing the same problem and @watari's fix works for me. To avoid modifying sonata_type_model_autocomplete.html.twig, I instead extended it, overrode the block sonata_type_model_autocomplete_ajax_request_parameters, and set the 'template' option of ModelAutocompleteType.

sirbaconjr commented 5 years ago

Same problem here. I did @jean-gui fix.

I think we need to go with solution 1

  1. condition will be changed sonata_type_model_autocomplete.html.twig:112 from {% if sonata_admin.admin is not null %} to {% if sonata_admin.admin is not empty %}

If we check FormTypeFieldExtension (that defines the sonata_admin field), in Line 51 the admin field inside sonata_admin is being set to null. Later on the same function its value is replace by the admin in the FieldDescription, see Line 66.

So far ok, but in Line 102, if the field is a child, then its value is replaced by false. Which is causing the error.

As far as I understand, the false value was added in this commit 20b4c3b68e42f165a441b42d3e5c35e598cc73e6. @rande can you help us understand what is the best solution in this case?

stale[bot] commented 4 years ago

Is this still relevant? If so, what is blocking it? Is there anything you can do to help move it forward?

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

watari commented 4 years ago

It is no longer relevant for me. I fixed it in project locally and published solution in comments here with questions related to it.

jean-gui commented 4 years ago

The workaround works well but the problem is still there.

github-actions[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

dilloncoffman commented 3 years ago

This is still an issue. @jean-gui what does your extended and overridden block look like exactly? Thanks for any help getting this solution to work!

jean-gui commented 3 years ago

@dilloncoffman It's been a while since I've looked into this, but here is what I have in my template, the block is copy-pasted from SonataAdmin/Form/Type/sonata_type_model_autocomplete.html.twig L100-139 with a single change, the last line in the snippet below:

{% extends '@SonataAdmin/Form/Type/sonata_type_model_autocomplete.html.twig' %}

{% block sonata_type_model_autocomplete_ajax_request_parameters %}
    return {
    //search term
    '{{ req_param_name_search }}': term,

    // page size
    '{{ req_param_name_items_per_page }}': {{ constant('App\\Kernel::ITEMS_PER_PAGE') }},

    // page number
    '{{ req_param_name_page_number }}': page,

    // admin
    {% if sonata_admin.admin is not empty %}
...
VincentLanglet commented 2 years ago

Should have been fixed by https://github.com/sonata-project/SonataAdminBundle/pull/7609