Spring 3 validation (JSR-303) @Min

10,809

You have to additionally explicitly specify Spring to validate your bean at the point of binding using @Valid annotation, can you confirm you are doing this. I am assuming the error that you are getting with 1.5 as the input is simply because 1.5 cannot be put into a Long field and is a binding exception, not a validation exception.

Share:
10,809

Related videos on Youtube

rapt
Author by

rapt

Updated on June 26, 2022

Comments

  • rapt
    rapt almost 2 years

    I am trying to validate an input field that is supposed to contain (if not empty) a natural number (i.e. a positive non-zero integer: 1, 2, 3, ....)

    I am using the following annotations:

    @Digits(integer=10, fraction=0)
    @Min(value = 1)
    private Long number;
    

    (Is this the best way to describe my constraint???)

    When I submit a number such as 1.5 I get a VALIDATION MESSAGE which is good. However when I submit an input such as -1 I do not get any VALIDATION MESSAGE. What do I miss?

    Thanks!

    P.S. Since my (other) Hibernate annotations for this field were on the getter of the field, I just move these two annotations to the getter as well (instead of being on the actual field). Did not help.

    EDIT

    I just read I might need to add <mvc:annotation-driven /> to my XML. I did it, however while starting the server I am getting the exception:

    org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/mvc] Offending resource: ServletContext resource [/WEB-INF/dispatcher-servlet.xml]

    I am not sure what it means and if I actually need (????) that annotation-driven tag... also, if I needed that annotation-driven tag in my xml, why did my other annotations (including one of the validation annotations) work without it?

  • rapt
    rapt over 12 years
    I have of course specified the @Valid annotation on my command object. My Controller's method looks like this: public ModelAndView list(HttpServletRequest request, HttpServletResponse response, @Valid Book book, BindingResult result) throws Exception {...} And NO, the error message I am getting is from the @Digits validation, not a binding exception. Without any validation being used, if I enter a negative integer number (e.g. -1), it acts as if I entered nothing (which is OK and what I expected). The weird thing is that with these two annotations, only the @Digits works.
  • rapt
    rapt over 12 years
    I meant integer in the mathematical sense. It can be larger than Integer so I am using Long. I think @Min(1) is just an abbreviation, anyway Eclipse suggests @Min(value = ).
  • Biju Kunjummen
    Biju Kunjummen over 12 years
    I saw your second edit, that error is because you need spring-webmvc jar(3.0+) in your classpath. mvc:annotation-driven is required, it causes the validator for JSR303 to be loaded up as a bean. Additionally in my local projects I have validation-api jar and hibernate's implementation hibernate-validator-4.1.0.Final.jar also present.
  • rapt
    rapt over 12 years
    Thanks! I've changed all my Spring jars from 3.0.0 to 3.0.5 and I @Min is working now. See above my comments to Willie Wheeler.