Spring MVC - Displaying Validation Errors outside form

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

Spring MVC - Displaying Validation Errors outside form

tdeverdiere
Hello,
I'am using Thymeleaf with Spring MVC.
I have a page with a form. When i submit my form i redisplay it. And for accessibility reason i would like to change the title of the page if there is an error, or if the processing complete.

So i tried something like this in the head tag:

<head>
    <form th:object="${candidatDTO}">
        <title th:if="${  #fields.hasErrors('*')}" th:text="#{candidat.infopers.title.error}">Saisie Informations Personelles</title>
        <title th:if="${! #fields.hasErrors('*')} and ${entryConfirmed == 'true'}" th:text="#{candidat.infopers.title.confirmed}"></title>
        <title th:if="${! #fields.hasErrors('*')} and ! ${entryConfirmed}" th:text="#{candidat.infopers.title}"></title>

--------------
but this generates the following error:

org.thymeleaf.exceptions.TemplateProcessingException: Cannot process expression for all fields "*" as no form model object has been established in the "form" tag (dossiercandidat/edit/personal_infos:5)
        at org.thymeleaf.spring3.util.FieldUtils.validateAndGetValueExpressionForAllFields(FieldUtils.java:178)
        at org.thymeleaf.spring3.util.FieldUtils.getBindStatus(FieldUtils.java:122)
        at org.thymeleaf.spring3.util.FieldUtils.hasErrors(FieldUtils.java:65)
        at org.thymeleaf.spring3.expression.Fields.hasErrors(Fields.java:46)

-----------
so i have done this trick, which works but then everything in the head go messy :
<head>
    <form th:object="${candidatDTO}">
        <title th:if="${  #fields.hasErrors('*')}" th:text="#{candidat.infopers.title.error}">Saisie Informations Personelles</title>
        <title th:if="${! #fields.hasErrors('*')} and ${entryConfirmed == 'true'}" th:text="#{candidat.infopers.title.confirmed}"></title>
        <title th:if="${! #fields.hasErrors('*')} and ! ${entryConfirmed}" th:text="#{candidat.infopers.title}"></title>
    </form>

-----
Is there a clean way to do that ?
Reply | Threaded
Open this post in threaded view
|

Re: Spring MVC - Displaying Validation Errors outside form

tdeverdiere
I've got this dirty solution with the remove attribute:
<head>
    <form th:object="${candidatDTO}" th:remove="tag">
Reply | Threaded
Open this post in threaded view
|

Re: Spring MVC - Displaying Validation Errors outside form

danielfernandez
Administrator
There currently is no direct way of exploring binding errors outside a <form> tag with a "th:object" attribute, because that is the specific scenario in which a Spring "BindStatus" object is produced for checking the existence of errors.

I imagine this could be enhanced by creating some sort of equivalent to the <spring:bind> or <spring:hasBindErrors> tags described in http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/spring.tld.html

Would you be so kind to add an enhancement ticket at https://github.com/thymeleaf/thymeleaf-spring3/issues ?

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

Re: Spring MVC - Displaying Validation Errors outside form

tdeverdiere
Thank you Daniel,
That is exactly the behaving i expect bind, and hasBindErrors