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

ModelType dont wor'k #7059

Closed epci closed 3 years ago

epci commented 3 years ago

Hello,

Environment

Sonata packages

sonata-project/admin-bundle dev-master 345daff dev-master 7f9faf8 The missing Symfony Admin Generator sonata-project/block-bundle 4.5.2 4.5.3 Symfony SonataBlockBundle sonata-project/cache 2.1.1 2.1.1 Cache library sonata-project/doctrine-extensions 1.12.0 1.12.0 Doctrine2 behavioral extensions sonata-project/doctrine-orm-admin-bundle dev-master 67ac215 dev-master 69346fc Integrate Doctrine ORM into the SonataAdminBundle sonata-project/exporter 2.6.0 2.6.2 Lightweight Exporter library sonata-project/form-extensions 1.9.0 1.9.0 Symfony form extensions sonata-project/twig-extensions 1.5.1 1.5.1 Sonata twig extensions

Symfony packages

symfony/asset v5.2.4 v5.2.4 Manages URL generation and versioning of web assets such as CSS stylesheets, JavaScript files and image files symfony/browser-kit v5.2.4 v5.2.4 Simulates the behavior of a web browser, allowing you to make requests, click on links and submit forms programmatically symfony/cache v5.2.4 v5.2.6 Provides an extended PSR-6, PSR-16 (and tags) implementation symfony/cache-contracts v2.2.0 v2.2.0 Generic abstractions related to caching symfony/config v5.2.4 v5.2.4 Helps you find, load, combine, autofill and validate configuration values of any kind symfony/console v5.2.5 v5.2.6 Eases the creation of beautiful and testable command line interfaces symfony/css-selector v5.2.4 v5.2.4 Converts CSS selectors to XPath expressions symfony/debug-bundle v5.2.4 v5.2.4 Provides a tight integration of the Symfony Debug component into the Symfony full-stack framework symfony/dependency-injection v5.2.5 v5.2.6 Allows you to standardize and centralize the way objects are constructed in your application symfony/deprecation-contracts v2.2.0 v2.2.0 A generic function and convention to trigger deprecation notices symfony/doctrine-bridge v5.2.5 v5.2.6 Provides integration for Doctrine with various Symfony components symfony/dom-crawler v5.2.4 v5.2.4 Eases DOM navigation for HTML and XML documents symfony/dotenv v5.2.4 v5.2.4 Registers environment variables from a .env file symfony/error-handler v5.2.4 v5.2.6 Provides tools to manage errors and ease debugging PHP code symfony/event-dispatcher v5.2.4 v5.2.4 Provides tools that allow your application components to communicate with each other by dispatching events and listening to them symfony/event-dispatcher-contracts v2.2.0 v2.2.0 Generic abstractions related to dispatching event symfony/expression-language v5.2.4 v5.2.4 Provides an engine that can compile and evaluate expressions symfony/filesystem v5.2.4 v5.2.6 Provides basic utilities for the filesystem symfony/finder v5.2.4 v5.2.4 Finds files and directories via an intuitive fluent interface symfony/flex v1.12.2 v1.12.2 Composer plugin for Symfony symfony/form v5.2.5 v5.2.6 Allows to easily create, process and reuse HTML forms symfony/framework-bundle v5.2.5 v5.2.6 Provides a tight integration between Symfony components and the Symfony full-stack framework symfony/http-client v5.2.4 v5.2.6 Provides powerful methods to fetch HTTP resources synchronously or asynchronously symfony/http-client-contracts v2.3.1 v2.3.1 Generic abstractions related to HTTP clients symfony/http-foundation v5.2.4 v5.2.4 Defines an object-oriented layer for the HTTP specification symfony/http-kernel v5.2.5 v5.2.6 Provides a structured process for converting a Request into a Response symfony/intl v5.2.4 v5.2.4 Provides a PHP replacement layer for the C intl extension that includes additional data from the ICU library symfony/mailer v5.2.5 v5.2.6 Helps sending emails symfony/maker-bundle v1.30.2 v1.30.2 Symfony Maker helps you create empty commands, controllers, form classes, tests and more so you can forget about writing boilerplate code. symfony/mime v5.2.5 v5.2.6 Allows manipulating MIME messages symfony/monolog-bridge v5.2.5 v5.2.5 Provides integration for Monolog with various Symfony components symfony/monolog-bundle v3.6.0 v3.7.0 Symfony MonologBundle symfony/notifier v5.2.4 v5.2.4 Sends notifications via one or more channels (email, SMS, ...) symfony/options-resolver v5.2.4 v5.2.4 Provides an improved replacement for the arrayreplace PHP function symfony/phpunit-bridge v5.2.4 v5.2.6 Provides utilities for PHPUnit, especially user deprecation notices management symfony/polyfill-intl-grapheme v1.22.1 v1.22.1 Symfony polyfill for intl's grapheme* functions symfony/polyfill-intl-icu v1.22.1 v1.22.1 Symfony polyfill for intl's ICU-related data and classes symfony/polyfill-intl-idn v1.22.1 v1.22.1 Symfony polyfill for intl's idn_to_ascii and idn_to_utf8 functions symfony/polyfill-intl-normalizer v1.22.1 v1.22.1 Symfony polyfill for intl's Normalizer class and related functions symfony/polyfill-mbstring v1.22.1 v1.22.1 Symfony polyfill for the Mbstring extension symfony/polyfill-php73 v1.22.1 v1.22.1 Symfony polyfill backporting some PHP 7.3+ features to lower PHP versions symfony/polyfill-php80 v1.22.1 v1.22.1 Symfony polyfill backporting some PHP 8.0+ features to lower PHP versions symfony/process v5.2.4 v5.2.4 Executes commands in sub-processes symfony/property-access v5.2.4 v5.2.4 Provides functions to read and write from/to an object or array using a simple string notation symfony/property-info v5.2.4 v5.2.4 Extracts information about PHP class' properties using metadata of popular sources symfony/proxy-manager-bridge v5.2.4 v5.2.4 Provides integration for ProxyManager with various Symfony components symfony/routing v5.2.4 v5.2.6 Maps an HTTP request to a set of configuration variables symfony/security-acl v3.1.1 v3.1.1 Symfony Security Component - ACL (Access Control List) symfony/security-bundle v5.2.5 v5.2.6 Provides a tight integration of the Security component into the Symfony full-stack framework symfony/security-core v5.2.5 v5.2.6 Symfony Security Component - Core Library symfony/security-csrf v5.2.4 v5.2.4 Symfony Security Component - CSRF Library symfony/security-guard v5.2.4 v5.2.4 Symfony Security Component - Guard symfony/security-http v5.2.5 v5.2.6 Symfony Security Component - HTTP Integration symfony/serializer v5.2.4 v5.2.4 Handles serializing and deserializing data structures, including object graphs, into array structures or other formats like XML and JSON. symfony/service-contracts v2.2.0 v2.2.0 Generic abstractions related to writing services symfony/stopwatch v5.2.4 v5.2.4 Provides a way to profile code symfony/string v5.2.4 v5.2.6 Provides an object-oriented API to strings and deals with bytes, UTF-8 code points and grapheme clusters in a unified way symfony/translation v5.2.5 v5.2.6 Provides tools to internationalize your application symfony/translation-contracts v2.3.0 v2.3.0 Generic abstractions related to translation symfony/twig-bridge v5.2.5 v5.2.6 Provides integration for Twig with various Symfony components symfony/twig-bundle v5.2.4 v5.2.4 Provides a tight integration of Twig into the Symfony full-stack framework symfony/validator v5.2.5 v5.2.6 Provides tools to validate values symfony/var-dumper v5.2.5 v5.2.6 Provides mechanisms for walking through any arbitrary PHP variable symfony/var-exporter v5.2.4 v5.2.4 Allows exporting any serializable PHP data structure to plain PHP code symfony/web-link v5.2.5 v5.2.5 Manages links between resources symfony/web-profiler-bundle v5.2.4 v5.2.6 Provides a development tool that gives detailed information about the execution of any request symfony/webpack-encore-bundle v1.11.1 v1.11.1 Integration with your Symfony app & Webpack Encore! symfony/yaml v5.2.5 v5.2.5 Loads and dumps YAML files

PHP version

$ php -v PHP 7.3.27 (cli) (built: Feb 2 2021 21:20:27) ( NTS ) Copyright (c) 1997-2018 The PHP Group Zend Engine v3.3.27, Copyright (c) 1998-2018 Zend Technologies with Zend OPcache v7.3.27, Copyright (c) 1999-2018, by Zend Technologies

Subject

I have an error when i try to use Sonata\AdminBundle\Form\Type\ModelType

Steps to reproduce

My configureFormFields in admin class

use Sonata\AdminBundle\Form\Type\ModelType;

class UserAdmin extends AbstractAdmin
{

    protected function configureFormFields(FormMapper $formMapper): void
    {
        $formMapper
            ->add('username', TextType::class, ['label' => 'Login'])
            ->add('email', EmailType::class, ['label' => 'Email'])
            ->add('plainPassword', PasswordType::class, array('required' => (!$this->getSubject() || is_null($this->getSubject()->getId())), 'label' => 'Password'))
            ->add('published', CheckboxType::class, ['label' => 'Actif', 'required' => false])
            ->add('firstname', TextType::class, ['label' => 'Prénom', 'required' => false])
            ->add('lastname', TextType::class, ['label' => 'Nom', 'required' => false])
            ->add('content', CKEditorType::class, array('required' => false, 'config_name' => 'simple', 'label' => false))
            ->add('groups', ModelType::class, ['expanded' => true, 'multiple' => true])
            ->add(
                'roles',
                ChoiceType::class,
                array(
                    'choices'  => $this->container->get('core.service.role')->getExistingRoles(),
                    'label'    => false,
                    'expanded' => true,
                    'multiple' => true,
                    'mapped'   => true,
                    'required' => false,
                )
            );
    }

}

Actual results

An exception has been thrown during the rendering of a template ("Field "groups" has already been rendered, save the result of previous render call to a variable and output that instead.").
Twig\Error RuntimeError in vendor/sonata-project/admin-bundle/src/Resources/views/CRUD/Association/edit_many_to_many.html.twig (line 110)
{% include '@SonataAdmin/CRUD/Association/edit_one_script.html.twig' %} {% else %}
{{ form_widget(form) }} => error is here
 {% if sonata_admin.field_description.associationadmin.hasRoute('create') and sonata_admin.field_description.associationadmin.hasAccess('create')

Expected results I have no problem when i use

            formMapper->add('groups', null, ['expanded' => true, 'multiple' => true]);

If you need an other information tell me before closing this issue

VincentLanglet commented 3 years ago

Hi,

I'm not sure, but what I understand is the error is coming from the fact the {{ form_widget(form) }} is called two times.

{% if sonata_admin.field_description.hasassociationadmin %}
    <div id="field_container_{{ id }}" class="field-container">
        <span id="field_widget_{{ id }}" >
            {% if sonata_admin.edit == 'inline' %}
                ...
            {% else %}
                {{ form_widget(form) }}
            {% endif %}

        </span>

        {% if sonata_admin.edit == 'inline' %}
            ...

        {% else %}
            <div id="field_container_{{ id }}" class="field-container">
                <span id="field_widget_{{ id }}" >
                    {{ form_widget(form) }}
                </span>

            </div>
        {% endif %}
    </div>
{% endif %}

So it's not fully related to the modelType, but more to the fact it's a ManyToMany and the template has an error.

Could you try to remove the

                <span id="field_widget_{{ id }}" >
                    {{ form_widget(form) }}
                </span>

part ? Does it fix your issue ?

If it fixed the bug, a PR could be open (on 3.x since the issue also exists).

epci commented 3 years ago

Hi Vincent,

When i remove this part, the error disappears (the widget with the "add" button appears). But i have an other error now when i click on the button on the ajax calls,

Variable "breadcrumbs_builder" does not exist.

in vendor/sonata-project/admin-bundle/src/Resources/views/standard_layout.html.twig (line 96)
 {% if _title is not empty %}  
 {{ _title|striptags|raw }}           
 {% else %}             
{% if action is defined %}      
{% for menu in breadcrumbs_builder.breadcrumbs(admin, action) %}      
{% if not loop.first %}    
{% if loop.index != 2 %}      
&gt;                            
{% endif %}
VincentLanglet commented 3 years ago

There is in CRUDController::addRenderExtraParams the code

        if (!$this->isXmlHttpRequest()) {
            $parameters['breadcrumbs_builder'] = $this->get('sonata.admin.breadcrumbs_builder');
        }

You could try to remove the condition.

I don't know why there is this condition ; but it feel weird that it never create issues before...

epci commented 3 years ago

for the second mistake, it's my fault. I created my own template for the edit view, which didn't extend the ajax_layout.html.twig template. On the other hand the first is corrected since I removed the following lines

<span id = "field_widget _ {{id}}">
{{form_widget (form)}}
</span>

I think a PR can be opened

VincentLanglet commented 3 years ago

I think a PR can be opened

Can you do it @epci ? :)

epci commented 3 years ago

Sorry, its' my first contribution on a project, i dont't know how i can do this ...

VincentLanglet commented 3 years ago

Sorry, its' my first contribution on a project, i dont't know how i can do this ...

So it's the best moment to start a first contribution !

You can

epci commented 3 years ago

Thank you for your explain, i think it's good now il make a PR on branch 3.0, is this change are updated branch master ?

VincentLanglet commented 3 years ago

il make a PR on branch 3.0, is this change are updated branch master ?

Yes, bugfix and new feature are done on 3.x and then we're merging 3.x into master.