• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Cannot get basic Spring validation to work. Why?

 
Bartender
Posts: 1757
17
  • 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
 
ranger
Posts: 17346
11
Mac IntelliJ IDE 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
Bartender
Posts: 1757
17
  • 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
Posts: 17346
11
Mac IntelliJ IDE 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
Bartender
Posts: 1757
17
  • 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
 
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
 
If you try to please everybody, your progress is limited by the noisiest fool. And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic