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

autocomplete input of sonata_type_model_autocomplete getting required attribute #4261

Closed RomainSanchez closed 7 years ago

RomainSanchez commented 7 years ago

Environment

DEV

Sonata packages

$ composer show sonata-project/*

sonata-project/admin-bundle              3.x-dev 17ae021 The missing Symfony Admin Generator
sonata-project/block-bundle              3.x-dev 89e4e57 Symfony SonataBlockBundle
sonata-project/cache                     1.x-dev a217ec0 Cache library
sonata-project/core-bundle               3.x-dev d584e67 Symfony SonataCoreBundle
sonata-project/datagrid-bundle           2.x-dev 60e0eac Symfony SonataDatagridBundle
sonata-project/doctrine-extensions       1.x-dev f2e173c Doctrine2 behavioral extensions
sonata-project/doctrine-orm-admin-bundle 3.x-dev db1e758 Symfony Sonata / Integrate Doctrine ORM int...
sonata-project/easy-extends-bundle       2.x-dev f54233b Symfony SonataEasyExtendsBundle
sonata-project/exporter                  1.x-dev c513edd Lightweight Exporter library
sonata-project/google-authenticator      1.x-dev ebc6987 Library to integrate Google Authenticator i...
sonata-project/intl-bundle               2.x-dev fc6be49 Symfony SonataIntlBundle
sonata-project/user-bundle               3.x-dev 85a50e8 Symfony SonataUserBundle

Symfony packages

$ composer show symfony/*

symfony/assetic-bundle     dev-master 0241b13 Integrates Assetic into Symfony2
symfony/monolog-bundle     2.x-dev b0146bd    Symfony MonologBundle
symfony/phpunit-bridge     dev-master f6d5082 Symfony PHPUnit Bridge
symfony/polyfill-apcu      dev-master 5d4474f Symfony polyfill backporting apcu_* functions to lower...
symfony/polyfill-iconv     dev-master cba36f3 Symfony polyfill for the Iconv extension
symfony/polyfill-intl-icu  dev-master 2d6e2b2 Symfony polyfill for intl's ICU-related data and classes
symfony/polyfill-mbstring  dev-master e79d363 Symfony polyfill for the Mbstring extension
symfony/polyfill-php54     dev-master 90e0858 Symfony polyfill backporting some PHP 5.4+ features to...
symfony/polyfill-php55     dev-master 03e3f03 Symfony polyfill backporting some PHP 5.5+ features to...
symfony/polyfill-php56     dev-master 1dd42b9 Symfony polyfill backporting some PHP 5.6+ features to...
symfony/polyfill-php70     dev-master 13ce343 Symfony polyfill backporting some PHP 7.0+ features to...
symfony/polyfill-util      dev-master 746bce0 Symfony utilities for portability of PHP codes
symfony/security-acl       dev-master 053b49b Symfony Security Component - ACL (Access Control List)
symfony/swiftmailer-bundle dev-master ad75109 Symfony SwiftmailerBundle
symfony/symfony            2.8.x-dev 4546377  The Symfony PHP framework

PHP version

$ php -v

PHP 5.6.26-2+deb.sury.org~xenial+1 (cli) 
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
    with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies

Subject

When editing an object with a REQUIRED form field of type sonata_type_model_autocomplete and using by_reference => false the form can't be submitted as the autocomplete input from the select from sonata_type_model_autocomplete has the required attribute, hence html5 validation is failing even though the field is not empty. If by_reference is not set to false it works correctly.

Got the same results in different admins.

If the by_reference option is not supposed to be used with sonata_type_model_autocomplete shouldn't an UndefinedOptionsException be thrown ?

Here is the relevant part of the dump of the formMapper

FormMapper {#11401 ▼
  #formBuilder: FormBuilder {#11395 ▼
    -children: array:43 [▶]
    -unresolvedChildren: array:43 [▼
      "createdBy" => array:2 [▶]
      "updatedBy" => array:2 [▶]
      "createdAt" => array:2 [▶]
      "updatedAt" => array:2 [▶]
      "isStrain" => array:2 [▶]
      "species" => array:2 [▶]
      "parent" => array:2 [▼
        "type" => "sonata_type_model_autocomplete"
        "options" => array:9 [▼
          "sonata_field_description" => FieldDescription {#11415 ▼
            #name: "parent"
            #type: "sonata_type_model_autocomplete"
            #mappingType: 2
            #fieldName: "parent"
            #associationMapping: array:19 [ …19]
            #fieldMapping: null
            #parentAssociationMappings: []
            #template: null
            #options: array:5 [ …5]
            #parent: null
            #admin: VarietyAdmin {#9661}
            #associationAdmin: VarietyAdmin {#11403 …79}
            #help: null
          }
          "class" => "Librinfo\VarietiesBundle\Entity\Variety"
          "model_manager" => ModelManager {#3413 ▶}
          "placeholder" => ""
          "by_reference" => false
          "property" => "name"
          "attr" => array:4 [ …4]
          "label_render" => false
          "label" => "librinfo.label.parent"
        ]
      ]
      "name" => array:2 [▶]

and the rendering call graph

SonataAdminBundle::standard_layout.html.twig 1829.98ms/92%
│                 └ LibrinfoMediaBundle:CRUD:edit.html.twig::block(form) 1502.26ms/75%
│                 │ └ SonataAdminBundle:CRUD:base_edit.html.twig::block(form) 1497.29ms/75%
│                 │ │ └ SonataAdminBundle:CRUD:base_edit_form.html.twig::block(form) 1497.26ms/75%
│                 │ │   └ SonataAdminBundle:CRUD:base_edit_form.html.twig::block(sonata_form_action_url) 42.27ms/2%
│                 │ │   └ SonataAdminBundle:CRUD:base_edit_form.html.twig::block(sonata_form_attributes)
│                 │ │   └ SonataAdminBundle:Helper:render_form_dismissable_errors.html.twig
│                 │ │   └ SonataAdminBundle:CRUD:base_edit_form.html.twig::block(sonata_pre_fieldsets)
│                 │ │   └ SonataAdminBundle:CRUD:base_edit_form.html.twig::block(sonata_tab_content) 1447.31ms/73%
│                 │ │   │ └ SonataAdminBundle:CRUD:base_edit_form_macro.html.twig::macro(render_groups) 708.33ms/36%
│                 │ │   │ │ └ SonataAdminBundle:Form:form_admin_fields.html.twig::block(checkbox_row)
│                 │ │   │ │ │ └ SonataAdminBundle:Form:form_admin_fields.html.twig::block(form_row)
│                 │ │   │ │ │   └ SonataAdminBundle:Form:form_admin_fields.html.twig::block(checkbox_label)
│                 │ │   │ │ │   │ └ SonataAdminBundle:Form:form_admin_fields.html.twig::block(checkbox_radio_label)
│                 │ │   │ │ │   └ SonataAdminBundle:Form:form_admin_fields.html.twig::block(checkbox_widget)
│                 │ │   │ │ │     └ form_div_layout.html.twig::block(checkbox_widget)
│                 │ │   │ │ │     │ └ form_div_layout.html.twig::block(widget_attributes)
│                 │ │   │ │ │     └ SonataAdminBundle:Form:form_admin_fields.html.twig::block(checkbox_label)
│                 │ │   │ │ │       └ SonataAdminBundle:Form:form_admin_fields.html.twig::block(checkbox_radio_label)
│                 │ │   │ │ └ SonataAdminBundle:Form:form_admin_fields.html.twig::block(form_row) 59.10ms/3%
│                 │ │   │ │ │ └ SonataAdminBundle:Form:form_admin_fields.html.twig::block(form_label)
│                 │ │   │ │ │ └ SonataDoctrineORMAdminBundle:Form:form_admin_fields.html.twig::block(sonata_type_model_widget) 58.60ms/3%
│                 │ │   │ │ │   └ SonataDoctrineORMAdminBundle:Form:form_admin_fields.html.twig::block(sonata_admin_orm_many_to_one_widget) 58.54ms/3%
│                 │ │   │ │ │     └ SonataDoctrineORMAdminBundle:CRUD:edit_orm_many_to_one.html.twig 30.28ms/2%
│                 │ │   │ │ │       └ form_div_layout.html.twig::block(choice_widget) 1.13ms/0%
│                 │ │   │ │ │       │ └ SonataAdminBundle:Form:form_admin_fields.html.twig::block(choice_widget_collapsed) 1.11ms/0%
│                 │ │   │ │ │       │   └ form_div_layout.html.twig::block(widget_attributes)
│                 │ │   │ │ │       │   └ form_div_layout.html.twig::block(choice_widget_options)
│                 │ │   │ │ │       └ SonataDoctrineORMAdminBundle:CRUD:edit_modal.html.twig
│                 │ │   │ │ │       └ SonataDoctrineORMAdminBundle:CRUD:edit_orm_many_association_script.html.twig
│                 │ │   │ │ └ SonataAdminBundle:Form:form_admin_fields.html.twig::block(form_row) 34.83ms/2%
│                 │ │   │ │ │ └ SonataAdminBundle:Form:form_admin_fields.html.twig::block(form_label)
│                 │ │   │ │ │ └ SonataAdminBundle:Form:form_admin_fields.html.twig::block(sonata_type_model_autocomplete_widget) 34.28ms/2%
│                 │ │   │ │ │   └ SonataAdminBundle:Form/Type:sonata_type_model_autocomplete.html.twig
│                 │ │   │ │ │     └ SonataAdminBundle:Form/Type:sonata_type_model_autocomplete.html.twig::block(sonata_type_model_autocomplete_ajax_request_parameters)
│                 │ │   │ │ │     └ SonataAdminBundle:Form/Type:sonata_type_model_autocomplete.html.twig::block(sonata_type_model_autocomplete_dropdown_item_format)
│                 │ │   │ │ │     └ SonataAdminBundle:Form/Type:sonata_type_model_autocomplete.html.twig::block(sonata_type_model_autocomplete_selection_format)
greg0ire commented 7 years ago

The issue was occuring both with AJAX and non-ajax since dd4695ee61464c252ca1ef61658787dfaae457d5, and was fixed with non-ajax in 05a128a94eaeb71421a9defef90b6049bb3c6167

greg0ire commented 7 years ago

Removing line 22 of https://github.com/sonata-project/SonataAdminBundle/blob/05a128a94eaeb71421a9defef90b6049bb3c6167/Resources/views/Form/Type/sonata_type_model_autocomplete.html.twig#L22 fixes the issue, not sure it is the right fix though.

greg0ire commented 7 years ago

I think adding a check on value|length would be better

greg0ire commented 7 years ago

The fix proposed by @fracsi here works even better.