1

hasError is not working; Spring 3 MVC Validation with Hibernate!

Maybe I am not doing this right but can someone tell me what my hasErrors() is false. I am puttting errors is my form but the hasErrors is never try?

Controaller:

@RequestMapping(value = "/save", method = RequestMethod.POST)
public String processValidatinForm(@ModelAttribute(" article") Article  article,
                BindingResult result) {

                System.out.println("In");

 if (result.hasErrors()) {
                    System.out.println("I found errors");
                       return "addArticle";
                }
                // Add the saved validationForm to the model

                articleService.addArticle( article);
                return "redirect:/articles.html";
            }

Form: @Entity @Table(name = "article") public class Article {

@Id
@GeneratedValue
@Column(name = "article_id")
private Long articleId;

@NotEmpty
@Size(max = 20)
@Column(name = "article_name", nullable = false, length=20)
private String articleName;

@NotEmpty
@Column(name = "article_desc", nullable = false)
private String articleDesc;

@Column(name = "date_added")
private Date addedDate;

public Article() {      
}

public Long getArticleId() {
    return articleId;
}

public void setArticleId(Long articleId) {
    this.articleId = articleId;
}

public String getArticleName() {
    return articleName;
}

public void setArticleName(String articleName) {
    this.articleName = articleName;
}

public String getArticleDesc() {
    return articleDesc;
}

public void setArticleDesc(String articleDesc) {
    this.articleDesc = articleDesc;
}

public Date getAddedDate() {
    return addedDate;
}

public void setAddedDate(Date addedDate) {
    this.addedDate = addedDate;
}   

}

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
SJS
  • 5,607
  • 19
  • 78
  • 105
  • 1
    It could be a problem with your Spring XML configuration, for example if the annotation support isn't configured correctly. Can you post your Spring XML configuration files? – gutch Mar 25 '11 at 04:26
  • it will not let me post it, please email me stutteringjohnsmith@gmail.com and I will email it to you – SJS Mar 25 '11 at 04:44
  • of course it lets you post it. Just indent it with 4 spaces. – Bozho Mar 25 '11 at 06:36
  • thanks @gutch it was the Spring XML coniguratio @Bozho how do accept the answers – SJS Mar 25 '11 at 13:23
  • @Stuttering John there is a tick right below the vote counter of the answer. tick it. – Bozho Mar 25 '11 at 14:21

1 Answers1

3

If JSR-303 validation is setup properly (you have hibernate-validator jar, and <mvc:annoatation-driven />) then you should just use @Valid @ModelAttribute(..) ..

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • @Hey Bozho I got it working with this code @RequestMapping(value = "/save", method = RequestMethod.POST) public String processValidatinForm(@Valid Article article, BindingResult result, Map model) { System.out.println("In"); if (result.hasErrors()) { System.out.println("I found errors"); return "addArticle"; } // Add the saved validationForm to the model articleService.addArticle( article); return "redirect:/articles.html"; } – SJS Mar 25 '11 at 13:27
  • @Bozho Could you take a look at my question? http://stackoverflow.com/questions/27886038/how-to-turn-on-annotation-driven-validation-in-spring-4 I always get the `errors.hasErrors()` return false. – smwikipedia Jan 12 '15 at 03:09