Announcement Announcement Module
Collapse
No announcement yet.
Annotation validation not working! Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Annotation validation not working!

    Here is my jsp form!
    Code:
    <div class="span-12 last">	
    		<form:form modelAttribute="account" action="${account.id}/makedit" method="post">
    		  	<fieldset>		
    				<legend>Account Fields</legend>
    				<p>
    					<form:label	for="name" path="name" cssErrorClass="error">Name</form:label><br/>
    					<form:input path="name" />   <form:errors path="name" />			
    				</p>
    				<p>	
    					<form:label for="balance" path="balance" cssErrorClass="error">Balance</form:label><br/>
    					<form:input path="balance"/> <form:errors path="balance" />
    				</p>
    				<p>
    					<form:label for="equityAllocation" path="equityAllocation" cssErrorClass="error">Equity Allocation</form:label><br/>
    					<form:input path="equityAllocation" /><form:errors path="equityAllocation" />
    				</p>
    				<p>	
    					<form:label for="renewalDate" path="renewalDate" cssErrorClass="error">Renewal Date</form:label><br/>
    					<form:input path="renewalDate" /><form:errors path="renewalDate" />
    				</p>
    				<p>	
    					<input type="submit" />
    				</p>
    			</fieldset>
    		</form:form>
    	</div>
    Here is my controller class
    Code:
    @RequestMapping(value="edit/{id}", method=RequestMethod.GET)
    	public String edit(@PathVariable Long id, Model model) {
    		Account account = this.accounts.get(id);
    		if (account == null) {
    			throw new ResourceNotFoundException(id);
    		}
    		model.addAttribute(account);
    		return "account/edit";
    	}
    	
    	
    	// AccountController.saveedit to Save edited account features to the hash table.
    	@RequestMapping(value="edit/{id}/makedit", method=RequestMethod.POST)
    	public String saveedit(@Valid Account account, @PathVariable Long id, BindingResult result){
    		if (result.hasErrors()) {
    			return "/edit/"+id;
    		}
    		account.setId(id);
    		this.accounts.put(account.getId(), account);
    		return "redirect:/account/" + account.getId();
    	}
    and here is my annotated account class!
    Code:
    @NotNull
    	@Size(min=1, max=25)
    	private String name;
    	
    	@NotNull
    	@NumberFormat(style=Style.CURRENCY)
    	private BigDecimal balance = new BigDecimal("1000");
    	
    	@NotNull
    	@NumberFormat(style=Style.PERCENT)
    	private BigDecimal equityAllocation = new BigDecimal(".60");
    
    	@DateTimeFormat(style="S-")
    	@Future
    	private Date renewalDate = new Date(new Date().getTime() + 31536000000L);
    Every time I give a bad value in the JSP form, for some reason.. it would not do any validation and throw exceptions!
    Sorry if this is a newbie question but I'm a noob around these parts.. please help as this project means a lot to me rite now..
    Thanks in advance

  • #2
    1) Use @ModelAttribute next to @Valid
    2) The BindingResult has to follow the ModelAttribute NOT the path variable.

    All this is explained in the reference guide I suggest a read of the web chapter.

    Comment


    • #3
      Originally posted by Marten Deinum View Post
      1) Use @ModelAttribute next to @Valid
      2) The BindingResult has to follow the ModelAttribute NOT the path variable.

      All this is explained in the reference guide I suggest a read of the web chapter.
      Thanks for the explanation.. However the 'ID' value is required for the process since I have to redirect to say "account/edit/{id}" view if the result.hasErrors==true.. How do I pass this and still not mess up the error view

      Comment


      • #4
        Read... I nowhere said you cannot use the path variable I only said (at least meant) you needed to reorder the attributes...

        Comment


        • #5
          Thanks a ton Marten... That cleared it and I successfully validated the form :-)!!
          Another quick question.. from the edit page.. i added a link to "Create account" page which should take me to
          localhost:8080/Spring-mvc-basic/account

          But from the edit page @ localhost:8080/Spring-mvc-basic/account/edit/{id} when I add a href tag to "/account".. it gives me undesired results.. I could add a hard link like "/spring-mvc-basic/account" but if someone changes the project name.. it would not work.. any way around this?

          Thanks again sirr

          Comment

          Working...
          X