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

Form checkbox handling with dynamic fields #71

Closed
mreiterer opened this issue Dec 1, 2014 · 8 comments
Closed

Form checkbox handling with dynamic fields #71

mreiterer opened this issue Dec 1, 2014 · 8 comments

Comments

@mreiterer
Copy link

I am using dynamic fields for a generic way to present entities. The actual values are in a map.

public class GenericEntity {
    protected Map<String, Object> properties = new LinkedHashMap<String, Object>();

Here is a snipped from my template:

<form th:object="${entity}" th:method="post" th:action="@{${#httpServletRequest.servletPath}}" id="add-entity-form" class="form-horizontal" role="form">

<div th:each="column : *{tableConfiguration.getColumns()}" class="form-group">
<input th:type="checkbox" th:field="*{properties[__${column.name}__]}" />

The checkbox is not bound correctly when using the pre-processing which is necessary for my dynamic fields.
Means: no hidden field is generated for spring mvc, the value in the map is not used to render the checked attribute of the input field.

@danielfernandez
Copy link
Member

I think you are missing single quotes in your expression. Shouldn't...

<input th:type="checkbox" th:field="*{properties[__${column.name}__]}" />

actually be:

<input th:type="checkbox" th:field="*{properties['__${column.name}__']}" />

?

@mreiterer
Copy link
Author

The approach works in general, also spring mvc databinding to the map. What i have to do manually is to convert the fields to the desired datatype.

The resulting html is:

<input type="checkbox" id="propertiesvdp" name="properties[vdp]" value="" />  

Your approach produces:

<input type="checkbox" id="properties&#39;vdp&#39;" name="properties[&#39;vdp&#39;]" value="" />  

Still no databinding, still no hidden field for spring mvc.

@mreiterer
Copy link
Author

my current workaround is:

    <input th:type="checkbox" th:name="|properties[${column.name}]|" th:value="true" th:checked="${entity.properties[__${column.name}__]}"/>
    <input type="hidden" th:name="|_properties[${column.name}]|" value="on" />   

danielfernandez added a commit to thymeleaf/thymeleaf-tests that referenced this issue Dec 3, 2014
@danielfernandez
Copy link
Member

@mreiterer
Copy link
Author

Thanks for effort and support.

Yes. i have a public getter method.
And yes - the workaround works OK without quotes.

  1. I do not understand your test - shouldn't the output include a hidden field with underscore prefix for spring mvc ?

  2. Can you add a test with a boolean property ?

entity.properties['boolField'] = true
Then i would expect the checked property set for the checkbox.

@danielfernandez
Copy link
Member

OK. The problem is in th:type. For some reason this works OK if you use type instead (which, by the way, you don't have any reason not to use). Will investigate this as a bug.

@mreiterer
Copy link
Author

Confirmed. Thanks. I am using th:type because i determine the type dynamically.

danielfernandez added a commit to thymeleaf/thymeleaf-tests that referenced this issue Dec 4, 2014
danielfernandez added a commit to thymeleaf/thymeleaf-tests that referenced this issue Dec 7, 2014
@danielfernandez
Copy link
Member

Fixed!

Updated 2.1.4-SNAPSHOT

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants