Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I don't know why the approach as mentioned in the question didn't work. I didn't make it work but walking through this <a href="http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/validation.html" rel="nofollow">document</a>, I found another approach that worked for me as per my requirements.</p> <p>I set the validator inside a method which was designated by the <code>@InitBinder</code> annotation.</p> <p>From <a href="http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/validation.html#validation-mvc-configuring" rel="nofollow">docs</a></p> <blockquote> <p>The Validator instance invoked when a @Valid method argument is encountered may be configured in two ways. First, you may call binder.setValidator(Validator) within a @Controller's @InitBinder callback. This allows you to configure a Validator instance per @Controller class:</p> </blockquote> <p>Specifically, in my requirements, the validation should only be performed while updating or inserting data into the database i.e when an associated submit button for those operations is pressed (there is a common button for both of these tasks (insert and update) in my application whose name is <code>btnSubmit</code>).</p> <p>The validation should be muted in any other case (for example, when the delete button is pressed). To meet this requirement, I have registered the validator as follows.</p> <pre><code>@InitBinder protected void initBinder(WebDataBinder binder, WebRequest webRequest) { if (webRequest.getParameter("btnSubmit") != null) { binder.setValidator(new TempValidator()); } else { binder.setValidator(null); } } </code></pre> <p>In this situation, the validator - <code>TempValidator</code> would only be set when the submit button whose name attribute is <code>btnSubmit</code> is clicked by the client.</p> <p>There is no need for xml configuration anywhere as well as auto-wiring.</p> <p>The exemplary controller class now looks like the following.</p> <pre><code>@Controller public final class TempController { @Autowired private TempService tempService; @InitBinder protected void initBinder(WebDataBinder binder, WebRequest webRequest) { if (webRequest.getParameter("btnSubmit") != null) { binder.setValidator(new TempValidator()); } else { binder.setValidator(null); } } //Removed the @Valid annotation before TempBean, since validation is unnecessary on page load. @RequestMapping(method = {RequestMethod.GET}, value = {"admin_side/Temp"}) public String showForm(@ModelAttribute("tempBean") TempBean tempBean, BindingResult error, Map model, HttpServletRequest request, HttpServletResponse response) { return "admin_side/Temp"; } @RequestMapping(method = {RequestMethod.POST}, value = {"admin_side/Temp"}) public String onSubmit(@ModelAttribute("tempBean") @Valid TempBean tempBean, BindingResult errors, Map model, HttpServletRequest request, HttpServletResponse response) { if (CustomizeValidation.isValid(errors, tempBean, TempBean.ValidationGroup.class, Default.class) &amp;&amp; !errors.hasErrors()) { System.out.println("Validated"); } return "admin_side/Temp"; } } </code></pre> <p>The <code>WebRequest</code> paramenter in the <code>initBinder()</code> method is not meant for handling the entire Http request as obvious. It's just for using general purpose request metadata.</p> <p>Javadocs about <code>WebRequest</code>.</p> <blockquote> <p>Generic interface for a web request. Mainly intended for generic web request interceptors, giving them access to general request metadata, not for actual handling of the request.</p> </blockquote> <p>If there is something wrong that I might be following, then kindly clarify it or add another answer.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload