Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Cannot get basic Spring validation to work. Why?

 
Mike London
Ranch Hand
Posts: 1190
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a JEE 6 project.

Per the Spring in Action, Third Edition, I inserted a @Size(min=2, max=6, message="First Name must be between 2 and 20 characters") in front of a private field like this in my domain class:

@NotEmpty
@Size(min = 2, max = 20, message="First Name must be between 2 and 20 characters")
private String firstName;

----

Then, in the JSP, I inserted the form:errors tag, like this:

<form:input type="text" path="firstName" id="firstName" class="textfield" />
<form:errors path="firstName" cssClass="error"/>

----

In the CSS I added the "cssClass" tag like this:

span.error {
color: red;
font-size: 8pt;
}

In the Controller class, I have a @Valid in the method that the JSP POSTs to.

----------------

Yet, when I run the class and intentionally leave the firstName field blank, I just go to the regular display page with no error displayed.

Clearly, I'm missing something.

What is still required to get the form validation to display the "message" next to the field that has violated the @Size validation?

Thanks

-m
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't you have to turn it on, by adding some bean in the xml to tell Spring that you are using @Valid and to search for them?

Mark
 
Mike London
Ranch Hand
Posts: 1190
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark Spritzler wrote:Don't you have to turn it on, by adding some bean in the xml to tell Spring that you are using @Valid and to search for them?

Mark


Right. I just figured this out.

I had to add

to the applicationContext.xml file and then add the required Jar files (like the hibernate validation library).

Thanks for your reply.

mike

 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike London wrote:
Mark Spritzler wrote:Don't you have to turn it on, by adding some bean in the xml to tell Spring that you are using @Valid and to search for them?

Mark


Right. I just figured this out.

I had to add

to the applicationContext.xml file and then add the required Jar files (like the hibernate validation library).

Thanks for your reply.

mike



Cool, thanks for posting the answer too. I knew something had to be declared, but not specifically what.

Mark
 
Mike London
Ranch Hand
Posts: 1190
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glad to help here.

Getting the validation working was one of those "little things" that should be quick to implement, but due to the lack of end-to-end examples I found ended up taking a couple days...

Thanks.

mike
 
Mark Secrist
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I remember when I was trying to get my first validation project working, it seemed like enabling this was a big mystery. In the end, all I had to do was enable <mvc:annotation-driven> + all the things you said you did. I haven't looked but I suspect that this tag actually enables the validator bean you mentioned.

Mark
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic