• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Cannot get basic Spring validation to work. Why?

 
Bartender
Posts: 1829
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1829
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1829
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
You showed up just in time for the waffles! 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
reply
    Bookmark Topic Watch Topic
  • New Topic