1

In a Spring application I am using Thymleaf as a template. Errors are not displayed on the page

<form method="post" th:action="@{/add-client}" th:object="${clientRequestDto}">
<div th:if="${param.success}">
   <div class="alert alert-success" role="alert">
         Client successfully added
    </div>
</div>
<div class="mb-3">
   <label class="form-label">Код</label>
   <input  class="form-control"  th:field="*{codeClient}" placeholder="160103650">
  <span class="validationError" th:if="${#fields.hasErrors('codeClient')}"
th:errors="*{codeClient}">Name error</span>
</div>
<div class="mb-3">
    <label class="form-label">Название торговой точки</label>
    <input  class="form-control" th:field="*{name}" placeholder="Ашан">
     <span class="validationError" th:if="${#fields.hasErrors('name')}"
th:errors="*{name}">Name error</span>
</div>

<input type="submit" value="Сохранить" class="btn btn-success">
<input type="reset" name="clearFilter" value="Очистить" class="btn btn-danger">
</form>

In DTO I use @NotEmpty to check fields

import jakarta.validation.constraints.NotEmpty;
import lombok.Data;
@Data
public class ClientRequestDto {
    @NotEmpty(message = "Вводите код")
    private String codeClient; 
    @NotEmpty(message = "Вводите название")
    private String name; 
  }

In the controller I use BindingResult for the error

@Controller
public class ClientController {

 @GetMapping("/clients")
    public String showClients(Model model){
     model.addAttribute("clientRequestDto", new ClientRequestDto());
     return "client";
 }

 @PostMapping("/add-client")
    public String addProduct(@ModelAttribute("clientRequestDto") @Valid ClientRequestDto clientRequestDto, BindingResult errors){
      
     if(errors.hasErrors()){
         return "client";
     }

     return "redirect:/clients?success";
 }

Pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.3.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.project</groupId>
    <artifactId>wms</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>wms</name>
    <description>Demo project for Spring Boot</description>
    <url/>
    <licenses>
        <license/>
    </licenses>
    <developers>
        <developer/>
    </developers>
    <scm>
        <connection/>
        <developerConnection/>
        <tag/>
        <url/>
    </scm>
    <properties>
        <java.version>21</java.version>
        <org.mapstruct.version>1.6.0.Beta1</org.mapstruct.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.33</version>
        </dependency>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct</artifactId>
            <version>${org.mapstruct.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.hibernate.validator</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>6.2.0.Final</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

I tried using Errors instead of BindingResult but it didn't help

If the fields are empty, Errors must return at least 5 error messages. But it returns 0 errors enter image description here

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.