1

Sorry for asking the simple question. I searched a lot , but can't find the exact solution.

In my spring bean class I have int field like (private int id) . I used @NotEmpty annotion.

I need to allow only numbers not any alphabets or string in the input field. What annotation I need to use.

I have tried the @NumberFormat(style = Style.NUMBER) , @Digits(fraction = 0, integer = 5) annotations and nothing working out.

Please suggest me the solution or any example for form validation...

Roman C
  • 49,761
  • 33
  • 66
  • 176
Human Being
  • 8,269
  • 28
  • 93
  • 136
  • What's exactly the problem? Spring allows you to enter arbitrary characters into a field bound to `int`? Are you sure? – axtavt Jan 18 '13 at 09:04
  • Take look at this: http://stackoverflow.com/questions/3721122/spring-validation-with-valid – madhead Jan 18 '13 at 09:07
  • @axtavt Thanks for your support . My need is to allow only the numbers in the text field.If I enter any string like "eee" or 34ue I need to show the error message as "Numbers only allowed" .What I should use ? – Human Being Jan 18 '13 at 09:09
  • Spring should not allow you to enter arbitrary strings there. If you want a customized error message, see http://stackoverflow.com/questions/4082924/jsr-303-type-checking-before-binding/4083198#4083198 – axtavt Jan 18 '13 at 09:14
  • @madhead Thanks for your suggestion. But it is not what I am looking for ?I mentioned my need in above comment.Please check it – Human Being Jan 18 '13 at 09:14
  • @Anand yes, I see your comments. But maybe you'll find such way of validation useful in future. Also Spring definitely does not allow you to input strings in number fields. You may also check client-side validation with JS in browser. – madhead Jan 18 '13 at 09:18
  • @axtavt Thanks for your reply.. I used message.properties and it is used for NotEmpty annotion... Now I need to know what annotation I need to use for allowing only numbers in text field? – Human Being Jan 18 '13 at 09:19
  • @madhead Thanks for reply... Oh I see.Any other annotation to allow only numbers in spring annotion or any other solution? – Human Being Jan 18 '13 at 09:22
  • 1
    @Anand Spring by itself cannot bind string to integer. No annotations needed! It will throw ParseException or smth like this. – madhead Jan 18 '13 at 09:23

1 Answers1

0

I suggest you to read the relevant part of the reference carefully. You create your validator which implements the Validator interface:

public class FooValidator implements Validator {

/**
* This Validator validates *just* Foo instances
*/
public boolean supports(Class clazz) {
    return Foo.class.equals(clazz);
}

public void validate(Object obj, Errors e) {
    ValidationUtils.rejectIfEmpty(e, "name", "name.empty");
    Foo foo = (Foo) obj;
    if (!isNumeric(foo.getFieldThatShouldBeNumeric())
    {
        e.rejectValue("fieldThatShouldBeNumeric", "notnumeric");
    }
}
}

then inject it, either 'locally' to the controller itself:

@Controller
public class MyController {

@InitBinder
protected void initBinder(WebDataBinder binder) {
    binder.setValidator(new FooValidator());
}

@RequestMapping("/foo", method=RequestMethod.POST)
public void processFoo(@Valid Foo foo) { ... }

or 'globally':

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <mvc:annotation-driven validator="globalValidator"/>

</beans>
abalogh
  • 8,239
  • 2
  • 34
  • 49