Skip to content

Commit

Permalink
Allow to disable icheck and to be able to use still choice field mask (
Browse files Browse the repository at this point in the history
  • Loading branch information
goetas authored Sep 21, 2023
1 parent f4f7e9c commit 5de1ab0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
27 changes: 27 additions & 0 deletions docs/cookbook/recipe_icheck.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)
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'],
// ...
],
])
;
}
13 changes: 7 additions & 6 deletions src/Resources/views/Form/form_admin_fields.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
<script>
jQuery(document).ready(function() {
var allFields = {{ all_fields|json_encode|raw }},
map = {{ map|json_encode|raw }},
showMaskChoiceEl = jQuery("{{ js_selector }}");
showMaskChoiceEl.on("{{ js_event }}", function () {
choice_field_mask_show(jQuery(this).val());
});
{% for js_event in js_events %}
showMaskChoiceEl.on("{{ js_event }}", function () {
choice_field_mask_show(jQuery(this).val());
});
{% endfor %}
function choice_field_mask_show(val) {
var controlGroupIdFunc = function (field) {
Expand Down

0 comments on commit 5de1ab0

Please sign in to comment.