Die Annotation @Valid löst keine Validierung aus, wenn die Postanforderung bei der Controller-Methode eintrifft. Was soll ich machen ?@Valid Formularvalidierung funktioniert nicht mit Thymeleaf Spring Boot
Eltern Controller:
public class ApplicationController {
@Autowired
private I18NService i18NService;
public I18NService getI18NService() {
return i18NService;
}
public void setI18NService(I18NService i18NService) {
this.i18NService = i18NService;
}
}
Der Layout-Controller:
public class ClientLayoutController extends ApplicationController{
@Autowired
private UserService userService;
@Autowired
private ClienteService clienteService;
protected ModelAndView getModelAndView(String aActiveMenu, String aI18n, String aViewName){
ModelAndView mav = new ModelAndView();
User user = userService.getAuthenticatedUser();
Cliente cliente = clienteService.getAuthenticatedCliente();
mav.addObject("active_menu",aActiveMenu);
mav.addObject("titulo",this.getI18NService().getMessage(aI18n));
mav.addObject("user",user);
mav.addObject("cliente",cliente);
mav.setViewName(aViewName);
return mav;
}
// Getters ans Setters...
}
Der Controller, der die Anfrage kommt:
@Controller
public class ClienteController extends ClientLayoutController {
@GetMapping("/client/profile")
public ModelAndView clientProfile() {
ModelAndView mav = this.getModelAndView("profile","client.profile","store/account-profile");
return mav;
}
@PostMapping("/client/profile")
public ModelAndView clientProfileUpdate(@Valid Cliente cliente,BindingResult bindingResult,Model model) {
System.out.println("sdfsdf "+cliente.getNome());
System.out.println(bindingResult.getErrorCount());
if(bindingResult.hasErrors()){
ModelAndView mav = this.getModelAndView("profile","client.profile","store/account-profile");
mav.addObject("cliente",cliente);
return mav;
}
return this.getModelAndView("profile","client.profile","store/account-profile");
}
}
Thymeleaf Form:
<form th:method="post" th:action="@{'/client/profile'}" th:object="${cliente}">
<div class="form-group">
<label for="nomecli" th:text="#{client.profile.name}">First Name</label>
<input th:field="*{nome}" type="text" id="nomecli" class="form-control" th:placeholder="#{client.profile.name}"/>
<span th:if="${#fields.hasErrors('nome')}" th:errors="*{nome}" class="text-danger"></span>
</div>
<button type="submit" class="btn btn-default btn-theme" th:inline="text"><i class="fa fa-check"></i> [[#{crud.save}]]</button>
</form>
Das Entity:
@Entity(name = "cliente")
public class Cliente {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "nome")
@NotEmpty(message = "Informe o nome")
private String nome;
// Other fields...
}
die bindingResult.getErrorCount() ist immer 0, obwohl ich ein leeres Formular veröffentlichen. Ich habe versucht, @NotNull und viele andere Dinge auf Google hinzuzufügen, aber keinen Erfolg.
Es hat nicht für mich funktioniert, aber danke für Ihre Hilfe! Hat das nicht mit der Kontrollvererbung zu tun? Denn das Einzige, was ich an den Beispielen, die ich online verfolgt habe, sehe, ist Folgendes. Ich werde später versuchen, wenn ich ein wenig mehr Zeit habe und Sie wissen lassen. – viniciusalvess
Ich habe das @ balag3 herausgefunden, überprüfe das später. – viniciusalvess