Home > Software design >  What does this expression mean in thymeleaf form validation?
What does this expression mean in thymeleaf form validation?

Time:09-05

In a tutorial I see, after validating a form, we want to display the error text on the html page using thymeleaf. The code written to show the errors of several fields in a form in order is as follows:

<ul>
     <li  role="alert" th:each="error : ${#fields.errors('contact.*')}" th:text="${error}"></li>
                </ul>

Also, the object to which we attach the form information is called contact.

Now I have some questions:

what is #? What is fields.errors? And why is .* used at the end of contact? If this is a regex, it should show errors if we remove .* from the end. But if we do not use .*, the errors will not be displayed. Why?

CodePudding user response:

"what is #? What is fields.errors?"

Don't separate those two pieces. The question should be "What is #fields.errors?"

Thymeleaf has a variety of so-called expression utility objects which are values which begin with a #.

They are basically classes in the Thymeleaf library which provide generally useful functions you may want to use in your templates.

As you can see from the documentation here:

Thymeleaf will offer us a set of utility objects that will help us perform common tasks in our expressions.

But for #fields.errors, the object #fields is not a part of the core Thymeleaf library. It is part of the Spring dialect of Thymeleaf.

Spring's version (dialect) of Thymeleaf adds a number of enhancements over standard Thymeleaf. This enhancement is aimed at making it easier for you to handle form validation errors in your templates.

You can see the JavaDoc for Fields here:

Expression Object for performing form-field-related operations inside Thymeleaf Standard Expressions in Spring environments.

You can also see from the JavaDoc that this Fields class has various useful methods for accessing any field validation errors provided by your Spring validation services.

One of these is:

public List<String> errors(String field)


"why is .* used at the end of contact?"

For the example of ${#fields.errors('contact.*')}, that is a Thymeleaf expression which uses this #fields.errors method to access all the error messages associated with all the fields in the contact object.

Here the * symbol is a wildcard for "all fields".

Don't read it as .* - a regular expression meaning "any character, zero one or many times".

Read it as a dot operator (.) separating the object name contact from the field name (in this case * for all fields).

You could replace * with a single field name from contact to get only the errors for that one field, instead of all (*) fields.


In the example in your question, the Thymeleaf expression is therefore accessing a list of error messages - the List<String> return value from the errors method it uses. It can therefore iterate over the messages in that list (if there are any).

And that is what the code does using th:each="error ... - using standard Thymleaf iteration logic.

  • Related