Cześć,
mam problem z walidacją.
Sprawdzałem inne wątki, na podstawie tego link walidacja mi wychodzi i jest tak jak powinno być.
Mianowicie, mam taką klasę Taco:
package com.example.tacocloud;
import lombok.Data;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import java.util.List;
@Data
public class Taco {
@NotNull
@Size(min = 5, message = "Nazwa musi składać się z przynajmniej pięciu znaków.")
private String name;
@NotNull
@Size(min = 1, message = "Musisz wybrać przynajmniej jeden składnik.")
private List<String> ingredients;
}
taką klasę OrderController:
package com.example.tacocloud.web;
import com.example.tacocloud.Order;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import jakarta.validation.Valid;
@Slf4j
@Controller
@RequestMapping("/orders")
public class OrderController {
@GetMapping("/current")
public String orderForm(Model model) {
model.addAttribute("order", new Order());
return "orderForm";
}
@PostMapping
public String processOrder(@Valid Order order, Errors errors) {
for (ObjectError error : errors.getAllErrors()) {
log.error("Validation error: " + error.getDefaultMessage());
}
if (errors.hasErrors()) {
return "orderForm";
}
log.info("Zamówienie zostało złozone: " + order);
return "redirect:/";
}
}
i taki plik design.html:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Zjedz z Łukim</title>
<link rel="stylesheet" th:href="@{../styles.css}"/>
</head>
<body>
<div class="container">
<div class="left-column">
<h1>Przygotuj własne Taco!</h1>
<form method="POST" th:object="${design}">
<div class="grid">
<div class="ingredient-group" id="wraps">
<h3>Wybierz rodzaj mąki:</h3>
<div th:each="ingredient : ${wrap}">
<input name="ingredients" type="checkbox" th:value="${ingredient.id}"/>
<span th:text="${ingredient.name}">SKŁADNIK</span><br/>
</div>
</div>
<div class="ingredient-group" id="proteins">
<h3>Wybierz mięso:</h3>
<div th:each="ingredient : ${protein}">
<input name="ingredients" type="checkbox" th:value="${ingredient.id}"/>
<span th:text="${ingredient.name}">SKŁADNIK</span><br/>
</div>
</div>
<div class="ingredient-group" id="cheeses">
<h3>Wybierz sery:</h3>
<div th:each="ingredient : ${cheese}">
<input name="ingredients" type="checkbox" th:value="${ingredient.id}"/>
<span th:text="${ingredient.name}">SKŁADNIK</span><br/>
</div>
</div>
<div class="ingredient-group" id="veggies">
<h3>Wybierz warzywa:</h3>
<div th:each="ingredient : ${veggies}">
<input name="ingredients" type="checkbox" th:value="${ingredient.id}"/>
<span th:text="${ingredient.name}">SKŁADNIK</span><br/>
</div>
</div>
<div class="ingredient-group" id="sauces">
<h3>Wybierz sosy:</h3>
<div th:each="ingredient : ${sauce}">
<input name="ingredients" type="checkbox" th:value="${ingredient.id}"/>
<span th:text="${ingredient.name}">SKŁADNIK</span><br/>
</div>
</div>
</div>
<div>
<h3>Nadaj nazwę przygotowanemu TACO:</h3>
<input type="text" th:field="*{name}"/>
<span class="validationError"
th:if="${#fields.hasErrors('name')}"
th:errors="*{name}">Nazwa Taco powinna składać się z minimum 5 znaków.</span>
<br/>
<button>Wyślij swoje zamówienie</button>
</div>
</form>
</div>
<div class="right-column">
<img class="logo" th:src="@{../TacoCloud.png}" alt="Logo"/>
</div>
</div>
</body>
</html>
Mimo tak skonstruowanego kodu po przejściu na http://localhost:8080/design
i nie wprowadzeniu żadnych danych strona przechodzi do /orders/current, a na konsoli dostaję zwrot:
Przetwarzanie projektu taco: Taco(name=, ingredients=null)
Bardzo proszę o pomoc