Error messages

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Error messages

aurelije
Hi,

I'm using Thymeleaf with Spring and have a problem with error messages. I have validator that rejects problems that are not connected with one field but with more than one filed. Eg. I have 2 date fields From and To, if From is after To that is empty range so validator is doing this:

p_errors.reject("validation.date.emptyrange", "Empty date range.");

I want to have messages like this showed in one place, but messages connected with particular fields showed next to that field.

To show near field I use this:

From: <input id="dateFrom" th:field="*{dateFrom}" name="dateFrom" type="text" class="date w_100px" style="width: 100px;"></input>
<ul th:if="${#fields.hasErrors('dateFrom')}" class="error">
      <li th:each="err : ${#fields.errors('dateFrom')}" th:text="${err}">Input is incorrect</li>
</ul>

But what to do to list messages not connected to the field? If I use '*' as selector I will duplicate error messages connected with particular field.

How to solve this problem?
Reply | Threaded
Open this post in threaded view
|

Re: Error messages

danielfernandez
Administrator
Hi,

I'm not sure whether this should be considered a Thymeleaf or a Spring MVC issue... can you do this without Thymeleaf (with Spring MVC + JSP)? If so... how? (it would be important to know, so that I can try to replicate it in Thymeleaf...)

Looking for a workaround... how about actually assigning that validator to a third field? (I mean, none of the two fields you are actually validating)... in an extreme case, this could even be a surrogate field, declared ad hoc only for holding this validation (and with no form input field at the HTML side)...

That way you could invoke its validation errors just by using the name of this third "surrogate" field, and show your errors wherever you want...

What do you think?

Regards,
Daniel.
Reply | Threaded
Open this post in threaded view
|

Re: Error messages

aurelije
I'm new with Spring MVC so I'm not sure if it can be done in Spring MVC with JSP templates.

Thanks for advices I will try to use them.