From 6f978433d63931e90e0112d1cf59907643fbb076 Mon Sep 17 00:00:00 2001 From: Asmir Mustafic Date: Wed, 20 Sep 2023 19:00:28 +0200 Subject: [PATCH] Allows to disable icheck and to be able to use still choice field mask with the expanded option --- docs/cookbook/recipe_icheck.rst | 27 +++++++++++++++++++ .../views/Form/form_admin_fields.html.twig | 13 ++++----- 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/docs/cookbook/recipe_icheck.rst b/docs/cookbook/recipe_icheck.rst index 82191dfd39..c00147cbb5 100644 --- a/docs/cookbook/recipe_icheck.rst +++ b/docs/cookbook/recipe_icheck.rst @@ -43,3 +43,30 @@ set data attribute ``data-sonata-icheck = "false"`` to this form element:: .. note:: You have to use false as string! ``"false"``! + +When using ``Sonata\AdminBundle\Form\Type\ChoiceFieldMaskType`` (or other types that inherit from it, such as ) +with the ``expanded``: ``true`` option (that renders the form type with checkboxes or radio buttons), +it is necessary to set the ``data-sonata-icheck`` attribute on its choice elements:: + + use Sonata\AdminBundle\Form\FormMapper; + use Sonata\AdminBundle\Form\Type\ModelType; + use Sonata\AdminBundle\Form\Type\ChoiceFieldMaskType; + + protected function configureFormFields(FormMapper $form): void + { + $form + ->add('category', ChoiceFieldMaskType::class, [ + 'expanded' => true, + 'placeholder_attr' => [ + // the placeholder (if any) needs also the data-sonata-icheck attr too since is rendered as + // checkbox or radio button + 'data-sonata-icheck' => 'false' + ], + 'choice_attr' => [ + 'val1' => ['data-sonata-icheck' => 'false'], + 'val2' => ['data-sonata-icheck' => 'false'], + // ... + ], + ]) + ; + } diff --git a/src/Resources/views/Form/form_admin_fields.html.twig b/src/Resources/views/Form/form_admin_fields.html.twig index 8b16ffa3e6..8d977d7798 100644 --- a/src/Resources/views/Form/form_admin_fields.html.twig +++ b/src/Resources/views/Form/form_admin_fields.html.twig @@ -475,20 +475,21 @@ file that was distributed with this source code. {% set main_form_name = id|slice(0, (id|length - name|length)-1) %} {% if expanded %} {% set js_selector = '#' ~ main_form_name ~ '_' ~ name ~ ' input' %} - {% set js_event = 'ifChecked' %} + {% set js_events = ['change', 'ifChecked'] %} {% else %} {% set js_selector = '#' ~ main_form_name ~ '_' ~ name %} - {% set js_event = 'change' %} + {% set js_events = ['change'] %} {% endif %}