Handling a null missing #fields object

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Handling a null missing #fields object

slmorton
Very new to Thymeleaf so please excuse any obvious errors.  I've been trying to search for this answer but actually struggling to come up with a google spell to get the answer.

I'm setting up a project using Thymeleaf and want to build a 'library' of fragments for standard controls. So all devs use the same. example below.  The code below works great most of the time, the problem is when the calling page has not been passed in an object from the controller . . the #fields utility object blows up with a parsing exception.  

Question is, can I somehow check if #fields is null/missing and bypass.  Or is there a better way of implementing my library of standard fragments for controls . . . what do others do?

Any help appreciated.


<th:block th:fragment="shortText (name, label, value)">
    <th:block th:replace="~{core/common :: text(name=${name}, label=${label}, value=${value}, type='text', size=20)}"></th:block>
</th:block>

<th:block th:fragment="mediumText (name, label, value)">
    <th:block th:replace="~{core/common :: text(name=${name}, label=${label}, value=${value}, type='text', size=50)}"></th:block>
</th:block>

calling. . .


<th:block th:fragment="text (name, label, value, type, size)">
    <label class="propertyLabel" th:for="${#strings.toLowerCase(name)}">
        Field
    </label>
    <input class="propertyValue" th:type="${type}" th:id="${#strings.toLowerCase(name)}"
           th:name="${name}" th:value="${value}"
           th:size="${size}" th:placeholder="${label}"/>

    <ul class="errorText" th:if="${#fields?.hasErrors(name)}">
        <li th:each="err : ${#fields?.errors(name)}" th:text="${err}"></li>
    </ul>

</th:block>