aspose file tools*
The moose likes Spring and the fly likes Cannot get basic Spring validation to work. Why? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Cannot get basic Spring validation to work. Why?" Watch "Cannot get basic Spring validation to work. Why?" New topic
Author

Cannot get basic Spring validation to work. Why?

Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1075
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

Joined: Feb 05, 2001
Posts: 17259
    
    6

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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1075
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

Joined: Feb 05, 2001
Posts: 17259
    
    6

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

Joined: Jul 12, 2002
Posts: 1075
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

Joined: Jul 01, 2003
Posts: 89
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
 
Consider Paul's rocket mass heater.
 
subject: Cannot get basic Spring validation to work. Why?