question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Spring / Thymeleaf checkbox compatibility issue

See original GitHub issue

Hello,

I’d like to let you know that there’s a compatibility issue with Thymeleaf templating engine (used in Spring framework).

The issue in question: Required structure of checkbox input in Materialize is following (<p> can be replaced for any other tag AFAIK):

<p>
    <input type="checkbox">
    <label> ... </label>
</p>

With CSS code used to target the label being:

[type="checkbox"] + label { ... }

Meaning “a label right after a checkbox”. Problem is that Thymeleaf adds additional <input type="hidden"> element after each checkbox input, breaking the checkbox + label selector. Following is actual Thymeleaf code I use in one of my projects. It uses a th:each loop to go over a list of items:

<div th:each="anArtwork : ${listOfArtworks}" class="row">
    <div class="col s12">
        <p>
            <input th:field="*{artworks}" th:value="${anArtwork.id}" type="checkbox" class="filled-in">
            <label th:for="${#ids.prev('artworks')}" th:text="${anArtwork.nameSk}"></label>
        </p>
    </div>
</div>

Which results into the following HTML code (generated repeatedly for each item in list with small differences in ID-related values):

<div class="row">
    <div class="col s12">
        <p>
            <input class="filled-in" type="checkbox" value="2" id="artworks2" name="artworks" />
            <input type="hidden" name="_artworks" value="on" />
            <label for="artworks2"> ... </label>
        </p>
    </div>
</div>

This of course breaks previous CSS selector for checkbox + label elements, as it adds additional element between them. Workaround I’m using adds additional selector to the CSS code in Materialize like this:

[type="checkbox"] + label,
[type="checkbox"] + [type="hidden"] + label { ... }

Which accounts for the additional hidden input and thus targets the required label. This is a Thymeleaf-specific solution only however and fastest one I could think of for my project.

My suggestion for a full compatibility fix/solution would be adding a mandatory wrapper element (like <p> or <span>) with a mandatory class (eg.: .material-checkbox) to the material checkboxes and target the inside checkbox and label elements using this class (.material-checkbox [type="checkbox"] and .material-checkbox label) instead of relying on the fact that the two elements will always be next to each other, which in Thymeleaf is not the case:

<p class="materialize-checkbox">
    <input type="checkbox">
    <label> ... </label>
</p>

.materialize-select [type="checkbox"] { ... }
.materialize-select label { ... }

I understand this add additional complexity and constraints on the Materialize framework, however as I said, a change on the part of Thymeleaf is not really possible. Or someone could devise a different solution, CSS/HTML is not really my primary focus, so there might be better options than what I suggested.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:9
  • Comments:7

github_iconTop GitHub Comments

5reactions
tgmarinhocommented, Jun 23, 2017

I did so, at the bottom of the page:

<script>
	$('input[type=hidden]').remove();
</script>

Its Works!

0reactions
andhencommented, Jun 23, 2017

tgmarinho: The complete solution is in my answer from 9 May. It does, however require recompiling Materialize. I’ll see if I can put in a pull request for it.

Your javascript workaround breaks quite a lot of things, including spring form handling and CSRF protection.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Tutorial: Thymeleaf + Spring
This tutorial explains how Thymeleaf can be integrated with the Spring Framework, especially (but not only) Spring MVC.
Read more >
Spring boot + thymeleaf in IntelliJ: cannot resolve vars
Support for Spring Boot autoconfigured MVC applications is complete, all bundled autoconfiguration view types are supported. Fix versions: 2017.3.
Read more >
handling multiple row submit with checkbox in thymeleaf and ...
Here is my controller. Here I am basically taking the List and iterating with Book type and getting each object and seeing that...
Read more >
Spring Thymeleaf Form Multi-Checkboxes Mapping with ...
In this quick post, I will share with you some code examples to display multiple checkboxes in HTML form with Spring MVC and...
Read more >
Views - User Interface Customization - CAS - Apereo Blog
Any files found in that module can be overridden by putting them in the same location under src/main/resources in the CAS overlay project....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found