Problem with BindingResult hasErrors() in validation.
I have this code:
@RequestMapping(value = "/entity", params = "form", method = RequestMethod.POST)
public String submit(@Valid @ModelAttribute Entity entity, BindingResult result) {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
entity.setCreatedBy(auth.getName());
if (result.hasErrors()) {
//Here the error of createdBy is null
return "entity/new";
} else {
entityService.save(entity);
return "redirect:/entity/list";
}
}
the entity class:
@Entity
@Table(name = "TABLE_X")
public class Entity implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@NotNull
@Column(name = "primary_key")
private String primaryKey;
@NotNull
@Column(name = "created_by")
private String createdBy;
//getters and setter
}
I need set the value of createdBy in controller but always show "may not be null" in view.
Please help.
Spring MVC 4, Hibernate Validator 5, Database Oracle 11g