0

What I would like to do is have a standard error message that uses an annotation to fill in a value on the said error message. The code should help explain.

I have a model POJO that has fields on it with JSR-303 annotations as such:

@Min(value = 1)
@Max(value = 9999)
private int someInt;

and PRESENTLY, I have two properties files: one to hold labels for my fields, and another to hold the actual messages:

label.someInt=Some Integer Field
Min="{0}" must be no less than {value}

Then I loop through stuff to build what I want:

for (FieldError a : result.getFieldErrors()) {
        boolean found = false;
        for (String b : a.getCodes()) {
            System.out.println(b);
            try {
                addErrorMessage(messageSource.getMessage(b,
                        new Object[] { new DefaultMessageSourceResolvable(
                            new String[] { "label." + a.getField() }
                        )},
                        null));
                found = true;
                break;
            } catch (Exception e){/* nom Exception */}
        }
        if (!found) {
            addErrorMessage(a.getDefaultMessage());
        }
    }

That's all fine and dandy, but I would like to be able to stick with the annotation-based way of doing things and add some sort of label on the fields. For instance:

@Min(value = 1)
@Max(value = 9999)
@Label(value = "Some Integer Field")
private int someInt;

Then I could write a helper function that would just use the annotation to label the error message I have in the properties file. I also need this to work with Spring-based validation errors such as typeMismatch. Fun stuff, that.

Thanks in advance for your help!

Andy
  • 8,749
  • 5
  • 34
  • 59

1 Answers1

2

Have a look at this forum: http://forum.springsource.org/archive/index.php/t-83353.html

or you can use the standard message keys supported by the DefaultMessageCodesResolver

It looks like it is already implemented.

Ralph
  • 118,862
  • 56
  • 287
  • 383