Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to disable icheck and to be able to use still choice field mask #8105

Merged
merged 2 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, such as )
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we don't need this extra-doc, no ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have updated it now. I think that the doc is helpful, it took me some time to realize that the attribute needs to be added to the choice elements and not to the outer type

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You still need to pass icheck => false on choice elements even if we don't check for this in the html then ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yea, the thing is that the icheck js tries to apply itself on all the radio inputs, except the one that have data-sonata-icheck set to false, but then using the expanded choice, the attributes to the choice need to be passed independently

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