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

List fields editable required value #5691

Closed peter-gribanov closed 4 years ago

peter-gribanov commented 5 years ago

Environment

Sonata packages

$ composer show --latest 'sonata-project/*'
sonata-project/admin-bundle              3.52.0 3.53.0 The missing Symfony Admin Generator
sonata-project/block-bundle              3.12.1 3.18.0 Symfony SonataBlockBundle
sonata-project/cache                     2.0.1  2.0.1  Cache library
sonata-project/core-bundle               3.17.0 3.17.0 Symfony SonataCoreBundle
sonata-project/datagrid-bundle           2.5.0  3.0.0  Symfony SonataDatagridBundle
sonata-project/doctrine-extensions       1.3.0  1.3.0  Doctrine2 behavioral extensions
sonata-project/doctrine-orm-admin-bundle 3.9.0  3.10.0 Symfony Sonata / Integrate Doctrine ORM into the SonataAdminBundle
sonata-project/exporter                  2.0.1  2.0.1  Lightweight Exporter library

Symfony packages

$ composer show --latest 'symfony/*'
symfony/monolog-bundle     v3.4.0  v3.4.0  Symfony MonologBundle
symfony/polyfill-apcu      v1.12.0 v1.12.0 Symfony polyfill backporting apcu_* functions to lower PHP versions
symfony/polyfill-ctype     v1.12.0 v1.12.0 Symfony polyfill for ctype functions
symfony/polyfill-iconv     v1.12.0 v1.12.0 Symfony polyfill for the Iconv extension
symfony/polyfill-intl-icu  v1.12.0 v1.12.0 Symfony polyfill for intl's ICU-related data and classes
symfony/polyfill-intl-idn  v1.12.0 v1.12.0 Symfony polyfill for intl's idn_to_ascii and idn_to_utf8 functions
symfony/polyfill-mbstring  v1.12.0 v1.12.0 Symfony polyfill for the Mbstring extension
symfony/polyfill-php56     v1.12.0 v1.12.0 Symfony polyfill backporting some PHP 5.6+ features to lower PHP versions
symfony/polyfill-php70     v1.12.0 v1.12.0 Symfony polyfill backporting some PHP 7.0+ features to lower PHP versions
symfony/polyfill-php72     v1.12.0 v1.12.0 Symfony polyfill backporting some PHP 7.2+ features to lower PHP versions
symfony/polyfill-util      v1.12.0 v1.12.0 Symfony utilities for portability of PHP codes
symfony/security-acl       v3.0.2  v3.0.2  Symfony Security Component - ACL (Access Control List)
symfony/swiftmailer-bundle v3.2.8  v3.2.8  Symfony SwiftmailerBundle
symfony/symfony            v3.4.31 v4.3.4  The Symfony PHP framework

PHP version

$ php -v
PHP 7.3.9 (cli) (built: Sep  3 2019 06:20:48) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.9, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.3.9, Copyright (c) 1999-2018, by Zend Technologies

Subject

Need add a required option to oblige to select a value from the list of valid values. Perhaps a related issue #5693.

Steps to reproduce

Make entity

class ContestantAdminView
{
    // ...

    public function setPermission(string $permission): void
    {
        $this->permission = $permission;
    }
}

Configure field as editable in admin service

class TestimonialAdmin extends BaseAdmin
{
    // ...

    protected function configureListFields(ListMapper $list_mapper): void
    {
        $list_mapper
            ->add('permission', 'choice', [
                'label' => 'Permission',
                'editable' => true,
                'required' => true,
                'choices' => Permission::change(),
                'template' => '@Frontend/Testimonial/admin/list_permission.html.twig',
                'catalogue' => 'messages',
            ])
        ;
    }

Go to admin. Change the permission, but do not select a value

image

Expected results

It is necessary to separate the case when we can use NULL as a value, and when not. In my case, NULL is not a valid value.

Actual results

Click submit and get 500 error because NULL comes

image

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.

peter-gribanov commented 4 years ago

Yes. This issue is still relevant. I still haven’t received any feedback.

greg0ire commented 4 years ago

I think using 'choice' is deprecated. Please use the type name like you would on a regular Symfony. That will help us understand what type you are actually using and debug further (I never remember what 'choice' translates to in the end)

peter-gribanov commented 4 years ago

@greg0ire see your documentation https://symfony.com/doc/master/bundles/SonataAdminBundle/reference/action_list.html#available-types-and-associated-options

peter-gribanov commented 4 years ago

@greg0ire using Symfony\Component\Form\Extension\Core\Type\ChoiceType does not affect the result

greg0ire commented 4 years ago

:thinking: We should probably update the docs, using the name is deprecated.

@greg0ire using Symfony\Component\Form\Extension\Core\Type\ChoiceType does not affect the result

But it does affect my understanding… I thought you were using a Sonata type, but this is a native form type. Is the bug that the native HTML5 required attribute is not rendered?

And why do you say

Need add a required option to oblige to select a value from the list of valid values.

You are using that option in your example

franmomu commented 4 years ago

I have taken a look at this and I think we are mixing things. This example is talking about the list, as @peter-gribanov said, the types available are in the docs. This has nothing to do with Symfony Type forms, but because this issue is about editable is a little bit confusing.

@greg0ire using Symfony\Component\Form\Extension\Core\Type\ChoiceType does not affect the result

I think this won't work because as I said this is about the list and editable uses this mapping.

That being said, the permission you already have in you entity is also one of the Permission::change() returns? maybe this is because it is not in that list and it does not select any.

peter-gribanov commented 4 years ago

All is well. I understood. The problem is on my side. Sorry.

The problem is that i use ValueObject and when casting object to a string, the label is returned, not the value.

Solution for me:

{# @Frontend/Testimonial/admin/list_permission.html.twig #}
{% extends admin.getTemplate('base_list_field') %}

{% set is_editable =
    field_description.options.editable is defined and
    field_description.options.editable and
    admin.hasAccess('edit', object)
%}
{% set x_editable_type = field_description.type | sonata_xeditable_type %}

{% block field_span_attributes %}
    {%- if is_editable and x_editable_type %}
        class="x-editable"
        data-type="{{ xEditableType }}"
        data-title="{{ field_description.label|trans({}, field_description.translationDomain) }}"
        data-pk="{{ admin.id(object) }}"
        data-url="{{ url }}"
        data-source="{{ field_description | sonata_xeditable_choices | json_encode }}"
        {# correct resolve value for x-editable #}
        data-value="{% if value.isAwaiting %}approved{% else %}{{ value.value }}{% endif %}"
    {%- endif -%}
{% endblock %}

{% block field -%}
    {%- set bem_modifier %}
        {%- if value.isAwaiting -%}
            label-danger
        {%- elseif value.isApproved -%}
            label-success
        {%- else -%}
            label-warning
        {%- endif -%}
    {%- endset -%}
    <span class="label {{ bem_modifier }}">{{ value | trans({}, field_description.options.catalogue) }}</span>
{%- endblock %}

Sorry again.