0

I am validating my form field with given piece of code.

//controller method
public String addBusiness(@Valid @ModelAttribute("myForm") MyForm myForm, ...)
{
   //logic will go here.
}

//form 
@Component
public class MyForm{
    @Pattern(regexp = "[0-9]{3,10}", message = "should be valid number")
    public String getZip_code()
    {
       return this.zip_code;
    }
}

Now I want same validation on zip_code in another method of controller like,

@RequestMapping(value = "${validation.url}", method = RequestMethod.GET)
@ResponseBody
public List<String> getCityList(@RequestParam(value = "zip_code", required = true) final String zip_code)
{
    //logic goes here
}

How is it possible?

Vishal Zanzrukia
  • 4,902
  • 4
  • 38
  • 82

1 Answers1

1

It's not. @Valid doesn't apply to @RequestParam annotated parameters. You can create a custom HandlerMethodArgumentResolver to do this or do the validation yourself in the method.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • Hi @SotiriosDelimanolis Thanks, can you give me any example of `HandlerMethodArgumentResolver` for such validation, if possible? – Vishal Zanzrukia Jun 23 '14 at 19:35
  • @VishalZanzrukia It's just an interface you implement and register. The implementation is use-case dependent. Google the class and you'll find examples like [this one](http://stackoverflow.com/questions/17331448/spring-handlermethodargumentresolver-not-executing). – Sotirios Delimanolis Jun 23 '14 at 20:13