0

Say my command looks like this

public class MyForm {
    @Max(99)
    private int mode;
    private MyObj myObj;

and my MyObj is

public class MyObj {
    private String myStr;
    private int myInt;

and my controller looks like this

@Controller
public class MyController {
    @RequestMapping(value = "/Anything.htm")
    public String AnythingMethod(@Valid MyForm myForm, .....

and my JSP looks like this

<form:input path="mode"/>
<form:input path="myObj.myStr"/>

how can I inject validation such as @NotNull @MAx @Min for myStr and myInt? how can I specify their error messages in messages.properties? Please help.

senderj
  • 400
  • 1
  • 9
  • Why can't you just add those annotations to these member variables like you did for `MyForm.mode`? – ivan.sim Sep 12 '14 at 04:02
  • @isim, It didn't work even with validation injection in MyObj. If I use `@Valid MyForm` in controller, it only validate MyForm.mode, not MyObj.myInt. If I use `@Valid MyObj` in controller, it only validate MyObj.myInt, not MyForm.mode. – senderj Sep 15 '14 at 03:49

1 Answers1

1

Unsure if the codes posted in your question are complete, you might be missing a few annotations. Also, since you mentioned in your comment that validations on some of the other fields work, I'm going to assume that you have all the dependencies sorted out.

So try this:

In your MyForm class:

public class MyForm {
    @Max(99, message="{myform.mode.max}")
    private int mode;

    @Valid // add @valid annotation
    private MyObj myObj;

In your MyObj class:

public class MyObj {
    @NotBlank(message="{myobj.mystr.notnull}")
    private String myStr;

    @Max(/* some value */, message="{myobj.mystr.max}")
    @Min(/* some value */, message="{myobj.mystr.min}")
    private int myInt;

In your MyController class:

@Controller
public class MyController {
    @RequestMapping(value = "/Anything.htm") /* add @ModelAttribute annotation */       
    public String AnythingMethod(@Valid @ModelAttribute('myForm') MyForm myForm, .....) 

As for specifying the error messages in a .properties file, put them all into a ValidationMessages.properties file, and add this file to the root of your classpath. (For e.g., /WEB-INF/classes, resources folder etc.).

Hope this helps.

Community
  • 1
  • 1
ivan.sim
  • 8,972
  • 8
  • 47
  • 63
  • thank you for your help. Problem resolved. I just don't know how to use @Valid at myObj. However, I am not able to make the message= parm work. I've added `myobj.mystr.max = myInt invalid` in spring's messages.properties (where my other msg are and working). I've also followed hibernate validator's config, adding ValidationMessges.properties to WEB-INFO\, with the same msg string. But both give my `{myobj.mystr.max}` as the message. Is there anything I've missed? – senderj Sep 18 '14 at 03:23
  • 1
    @senderj All the validation error messages should go into `ValidationMessages.properties`. You can add this file to wherever you are storing your `messages.properties` file. Also, keep in mind, it's `WEB-INF`, not `WEB-INFO`. – ivan.sim Sep 18 '14 at 03:30
  • sorry, WEB-INFO was a typo. NetBeans project explorer only exposes WEB-INF, so the ValidationMessages.properties was added to WEB-INF, not WEB-INF/classes. I have to use ant script to copy over. Is there any xml config method to specify the name and location of the file?? – senderj Sep 19 '14 at 03:46
  • The only way to change the file name and location is to specify the configurations using hibernate-validation ResourceBundleMessageInterpolator and its bundle locator API https://forum.hibernate.org/viewtopic.php?f=26&t=1011623 – ivan.sim Sep 19 '14 at 04:45
  • it's abit overkill in my case, I'll stay with ant copy file approach for simplicity. Thanks for you help. – senderj Sep 22 '14 at 01:46