0

So I just set up hibernate validation using annotation.

Basically something like this

 @NotNull(message="please enter your first name")
 @Size(min=5, max=25, message = "your name is under or over the allotted size)
 String lastName

The validation itself works fine for the most part. The size component will trigger on submit. However, if I load the page it's validating the NotNull validation will trigger the moment it loads reminding the user to fill in the blank input. Since this is triggered in the bean, and I'm still rather new to Spring MVC I was wondering if there was anything I could do to have it trigger on submit instead of load.

Controller code...

@RequestMapping(value = "/App")

   public ModelAndView loadAppPage(@RequestParam(value = "Sal", required = false) String Sal,

   @RequestParam(value = "name1", required = false) String name1,

   @RequestParam(value = "name2", required = false) String name2,


   @ModelAttribute("newApp")@Valid App newApp, BindingResult result){



         ModelAndView mav = new ModelAndView();

         String timestamp = new java.text.SimpleDateFormat("MM/dd/yyyy h:mm:ss a").format(new Date());



         if(sal != null || name1 != null || name2 != null |){


              newContact.setSal(Sal);

                newContact.setname1(name1);

                newContact.setname2(name2);



                new AppValidator().validate(newApp, result);

                if(result.hasErrors()){

                       return new ModelAndView("App", "newApp", newApp);

                }else{

                       appService.addApp(newApp);



   /*                  mav.addObject("newApp", new App());*/

                       mav.setViewName("redirect:App.html");

                       return mav;

                }

         }

         mav.addObject("newApp", newApp);

         mav.setViewName("App");

         return mav;



   }
Jack Parker
  • 547
  • 2
  • 9
  • 32
  • You're loading the page using an http get? If you do not trigger the validation, it will not occur. – Jazzwave06 Mar 09 '15 at 14:01
  • OK, maybe I didn't phrase this correctly. The validation works. The bean is setup correctly. THere are getters and setters in the bean. WHat I am saying is that it triggers on the entry page of my form on load for the notnull validation meaning that when it loads the inputs are blank, thus null, and an error even though they would not have had achance to fill in the value yet. – Jack Parker Mar 09 '15 at 14:09
  • I know it works. However, the validation does not work out of the box with the annotation on your pojos. You need to trigger a validator to get the validation. There's two things that could be happening: the framework you use auto-validate on POST request, or the validation messages are embedded in the page for javascript to use. If you're using GET requests, then make sure that the validation is not triggered in javascript on document ready. – Jazzwave06 Mar 09 '15 at 14:12
  • Oh ok that makes more sense thank you. – Jack Parker Mar 09 '15 at 14:15
  • OK, let's just say I'm trying to fix this in my controller. If this is on page load or something. Is there a way to clear out the errors? if (result.hasErrors() { } – Jack Parker Mar 09 '15 at 18:18
  • Post your controller's code. – Jazzwave06 Mar 09 '15 at 18:26
  • Included controller code. – Jack Parker Mar 09 '15 at 18:44
  • The problem is you didn't separate GET request from POST requests. You don't validate an object on GET requests. Your application clearly has a "separation of concern" concern. – Jazzwave06 Mar 09 '15 at 18:45
  • But this is the code for an entry page, so it gets the information, and is supposed to validate if it's supposed to be correct before submitting it. So what am I supposed to do? – Jack Parker Mar 09 '15 at 18:49
  • http://en.wikipedia.org/wiki/Command%E2%80%93query_separation You are trying to do both in the same method. This is a code smell. You need to separate the query (a GET request to obtain the application page) and the command (a POST request to submit data) You don't need to validate input on GET requests. – Jazzwave06 Mar 09 '15 at 18:54
  • With your scenario you can get away by manually triggering validation, and stay away from the `@Valid` here is an SO discussion about that http://stackoverflow.com/questions/28702809/how-to-manually-trigger-spring-validation/28704025#28704025 – iamiddy Mar 09 '15 at 19:44

0 Answers0