Reputation: 73
I can'figure out why my validation sample code doesn't work ! This is my object :
@PasswordMatches
public class UtilisateurInscriptionEnCoursDto implements Serializable {
private static final long serialVersionUID = -4445329534944784344L;
/** The id. */
private Long id;
@NotNull(message = "Saisissez votre nom")
@NotEmpty
private String nom;
@NotNull
@NotEmpty(message = "Saisissez votre prénom")
private String prenom;
@NotNull
@NotEmpty(message = "Entrez un mot de passe")
private String password;
private String matchingPassword;
@ValidEmail
@NotNull
@NotEmpty(message = "Saisissez votre adresse mail")
private String email;
And this is my Controller :
@GetMapping("/inscription")
public String afficherFormulaireInscription(WebRequest request, Model model) {
model.addAttribute("user", new UtilisateurInscriptionEnCoursDto());
return "inscription";
}
@PostMapping("/inscription")
public String enregistrerCompteUtilisateur(@ModelAttribute("user") @Valid UtilisateurInscriptionEnCoursDto utilisateurInscriptionEnCoursDto,
BindingResult result, Model model, HttpServletRequest request) {
if (result.hasErrors()) {
return "inscription";
}
try {
this.gestionUtilisateuFacade.verifierEmailInscriptionUtilisateur(utilisateurInscriptionEnCoursDto);
} catch (UtilisateurExistantException ueEx) {
String errMessage = this.messages.getMessage("message.regError", null, request.getLocale());
model.addAttribute("message", errMessage);
return "inscription";
} catch (RuntimeException ex) {
LOGGER.warn("Impossible de s'inscrire", ex);
return "inscription";
}
return "succesInscription";
}
The try/catch bloc works well, but I can't succeed having result.hasErrors() == true, even if I leave all fields blank.
Any idea to help
Thx a lot.
Upvotes: 2
Views: 215
Reputation: 836
I added my comment as answer:
If you are using spring boot 2.3+ maybe you need dependency spring-boot-starter-validation.
Upvotes: 2