Skip to content

Model Type: New created entity does not get selected on sortable collections #8201

@BA-JBI

Description

@BA-JBI

Environment

Sonata packages

show

$ composer show --latest 'sonata-project/*'
Direct dependencies required in composer.json:
sonata-project/translation-bundle        dev-symfony7 cbd1ec0 dev-symfony7 cbd1ec0 SonataTranslationBundle
sonata-project/user-bundle               5.x-dev 2d82bcb      5.x-dev 2d82bcb      Symfony SonataUserBundle

Transitive dependencies not required in composer.json:
sonata-project/admin-bundle              4.31.0               4.31.0               The missing Symfony Admin Generator
sonata-project/block-bundle              5.1.0                5.1.0                Symfony SonataBlockBundle
sonata-project/doctrine-extensions       2.4.0                2.4.0                Doctrine2 behavioral extensions
sonata-project/doctrine-orm-admin-bundle 4.17.1               4.17.1               Integrate Doctrine ORM into the SonataAdminBundle
sonata-project/exporter                  3.3.0                3.3.0                Lightweight Exporter library
sonata-project/form-extensions           2.4.0                2.4.0                Symfony form extensions
sonata-project/intl-bundle               3.2.0                3.2.0                Symfony SonataIntlBundle
sonata-project/media-bundle              4.13.0               4.13.0               Symfony SonataMediaBundle
sonata-project/twig-extensions           2.4.0                2.4.0                Sonata twig extensions

PHP version

show

``` $ php -v PHP 8.3.10 (cli) (built: Aug 2 2024 16:00:00) (NTS) ```

Subject

$form->add('references', ModelType::class, [
    'multiple' => true,
    'sortable' => true, // <-- This produces the issue
];

image

When adding new Entity via "New" button, entity is created correctly but new created entity does not get selected automatically.

Problem

Because of

<input type="hidden" name="{{ full_name }}" id="{{ id }}" value="{{ value|join(',') }}" />
the rendered field is just a hidden input type.

success: function(html) {
jQuery('#field_container_{{ id }}').replaceWith(html);
var newElement = jQuery('#{{ id }} [value="' + data.objectId + '"]');
if (newElement.is("input")) {
newElement.attr('checked', 'checked');
} else {
newElement.attr('selected', 'selected');
}
jQuery('#field_container_{{ id }}').trigger('sonata-admin-append-form-element');
}

The edit_many_script does not handle the returned id value correctly.

Proposed solution

jQuery('#field_container_{{ id }}').replaceWith(html);
var newElement = jQuery('#{{ id }} [value="' + data.objectId + '"]');

if (newElement.length) {
    if (newElement.is("input")) {
        newElement.attr('checked', 'checked');
    } else {
        newElement.attr('selected', 'selected');
    }
} else {
    var selections = jQuery('#{{ id }}').val().split(',');
    selections.push(data.objectId);
    jQuery('#{{ id }}').val(selections.filter((val) => val.length > 0).join(','));
}

jQuery('#field_container_{{ id }}').trigger('sonata-admin-append-form-element');

Already tested it works

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions