title: A good practice to present service level validation errors to the user using Spring MVC
body: <p>Are there some good practices to present service layer validation errors using Spring MVC after "shallow" user input validation has been performed using Spring MVC Validators? For example, having this code:</p>
  
  <pre><code>@Autowired
  private UserService userService;
  
  @RequestMapping(value = "user/new", method = RequestMethod.POST)
  public String createNewUser(@ModelAttribute("userForm") UserForm userForm, BindingResult result, Model model){
      UserFormValidator validator = new UserFormValidator(); //extending org.springframework.validation.Validator
      validator.validate(userForm, result);
  
      if(result.hasErrors()){
          model.addAttribute("userForm", userForm);
          return "user/new";
      }
  
      // here, for example, the user already might exist
      userService.createUser(userForm.getName(), userForm.getPassword());
  
      return "redirect:user/home";
  } 
  </code></pre>
  
  <p>While it may seem trivial having this code as an example, it seems to be a delicate story to me when validation at service layer is a complex task. Despite being an absurd scenario, the UserService might take a list of users to create, and if one of them already exists, then the view tier must somehow be notified about which of them is not valid (e.g. does already exist).</p>
  
  <p>I am looking for a good practice how to design a piece of code, which makes it possible to </p>
  
  <p>1) handle validation errors at the service layer having complex data as input, and  </p>
  
  <p>2) to present these validation errors to the user </p>
  
  <p>as easy as possible. Any suggestions?</p>