thymeleaf object binding can ignore case for form fields?

Issue

Newly started using thymeleaf for my application. I am building a dynamic form from controller where form fields are created as a list of map. Each key value pair is iterated as below.

  <form th:action="#" th:object="${foo}" method="post">...
  <input type="text" id=${element.key}   
        th:name=${element.key} th:value="${element.value}"/> ...</form> 

My Pojo has properties in lower case as following java standard. But when building the key property my code uses the Capital letters from external file and also in order to have a better readability on UI not converting from upper case to lower case conversion in
mydata.put("KEY", 90);.

Upon submitting the page, my form elements not binding with propery key of Foo class due to KEY to key is not mapping. Getting null for the form fields due to input form field name as ‘KEY’. I suspect the reason for getting null values for input text fields entered in html pages are due to case-sensitive of object properties in Foo class.

class Foo { private String key; //KEY can be a fix but naming convention is missing. }

Is there any thymeleaf tags available to ignore cases when binding html-elements to objects ? Or shall I use any JavaScript tweak upon submitting the form ? Like sending lower case and converting to upper case on form load to display ?

Solution

you can try to use ${#strings.toLowerCase(element.key)}

<form th:action="#" th:object="${foo}" method="post">...
    <input type="text" th:id="${#strings.toLowerCase(element.key)}" th:name=${#strings.toLowerCase(element.key)} th:value="${element.value}"/> ...
</form> 

Answered By – Rafael da Silva

Answer Checked By – Pedro (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.