0
Today I can validate attributes errors of my class that are not complex objects (other objects of my model that are related to it), but what happens is that the "validation bean" validates the attributes of these objects, but, only returns to Bindingresult the validation errors of the simple attributes of my class (int, Integer, String...), the complex attributes of my class (Address, Contact, Documents...) that have other attributes to be validated, are not being returned by Bindingresult in my controller, I know they are being validated correctly, why Hibernate tries to persist and generates an Exception by not meeting the validation requirements.
My Rental Model Class:
package model;
import java.io.Serializable;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.validation.constraints.Digits;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import annotations.JSONComponent;
@Entity
public class Locador implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Digits(integer=9, message="O campo código deve ter pelo menos 3 números até 9 números !", fraction = 0)
@Min(value=1, message="O campo código não pode ter um número menor que 1.")
@Max(value=999999, message="O campo código pode ser um número entre 1 e 999999.")
@NotNull(message="O campo código não pode estar vazio !")
private Long codigo;
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private Documentos documentos;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private List<Endereco> enderecos;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private List<Contato> contatos;
// outros atributos e omitidos...
My Model Address class:
package model;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.validation.constraints.NotNull;
@Entity
public class Endereco implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String logradouro;
@NotNull(message="O campo número não pode estar vazio !")
private Integer numero;
//Outros atributos omitidos...
I did a test on my controller and went through all the mistakes of validation, but only validation errors of attributes appear "simple" in my class :
@PostMapping
public ModelAndView cadastrarLocador(@Valid Locador locador, BindingResult errors, RedirectAttributes redirectAtriAttributes)
throws Exception {
//aqui varro os erros de validação mas só aparece para mim os referentes ao "código" da minha classe principal....
if (errors.hasErrors()) {
errors.getGlobalErrors().forEach(System.out::println);
errors.getAllErrors().forEach(System.out::println);
return form();
}
// locadorService.salvar(locador);
redirectAtriAttributes.addFlashAttribute("locadorCadastrado", locador.getNome());
return new ModelAndView("redirect:/carregar/carregarLocador");
}
private ModelAndView form() {
return new ModelAndView("forward:/carregar/carregarLocador");
}
I got a behavior similar to what I wanted by creating my own validation, but unfortunately this way I can’t change the message depending on the validation error.
– Gabriel Martins