Announcement Announcement Module
Collapse

Spring Modules forum decommissioned in favor of Spring Extensions

As the Spring Modules project has been replaced by the Spring Extensions (http://www.springsource.org/extensions) project, this forum has been decommissioned in favour of Spring Extensions one at:
http://forum.springsource.org/forumdisplay.php?f=44

Please see the Spring Extensions home page for a complete list of current projects in Java, .NET and ActionScript. You can also propose one if you want.

Cheers,
Costin Leau
SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
http://twitter.com/costinl
See more
See less
[validation] BeanValidator does not seem to lookup "message" in providedMessageSource Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • [validation] BeanValidator does not seem to lookup "message" in providedMessageSource

    Hi again,

    I'm having some problems with BeanValidator NOT looking up error message in the provided MessageSource. Section 16.2.3.3. "Localization" states that it is support as long as arguments are not used, but that might have been in another context. Section 16.2.1.1. "Rule Configuration" and Table 16.3 in the documentation (for 0.7) does not mention anything about error messages being resolved by MessageSource.

    Does not BeanValidator support usage of MessageSource or am I missing out on something?

    Also, can the MessageSource be fully qualified with package name (e.g. "se.stickybit.test.MyResourceBundle")?

    The test below outputs:

    ERROR COUNT: 1
    Field error in object 'target' on field 'simple': rejected value [2short]; codes [SimpleBeanTest.SimpleBean.simple[length].target.simple,SimpleBeanTest.SimpleBean.simple[length].simple,SimpleBeanTest.SimpleBean.simple[length].java.lang.String,SimpleBeanTest.SimpleBean.simple[length]]; arguments [se.stickybit.test.rmiapi.beans.SimpleBeanTest$Simp leBean@b1c5fa,10,255]; default message [invalid_length]
    simple


    Code:
    public class SimpleBeanTest
        extends TestCase
    {
        public SimpleBeanTest(final String arg0)
        {
            super(arg0);
        }
    
        public void testValidation() throws Exception
        {        
            SimpleBean sb  = new SimpleBean();
            sb.setSimple("2short");
    
            GenericApplicationContext appCtx = new GenericApplicationContext();
           
            ResourceBundleMessageSource rbms = new ResourceBundleMessageSource();
            rbms.setAlwaysUseMessageFormat(true);
            rbms.setUseCodeAsDefaultMessage(false);
            rbms.setBasename("MyResourceBundle.properties");
            appCtx.getBeanFactory().registerSingleton("messageSource", rbms);
            appCtx.refresh();
            
            AnnotationBeanValidationConfigurationLoader l = new AnnotationBeanValidationConfigurationLoader();
            l.setCheckValidatableAnnotation(true);
            l.setApplicationContext(appCtx);
            l.loadConfiguration(sb.getClass());
            
            BeanValidator bv = new BeanValidator();
            bv.setConfigurationLoader(l);
            
            // errors.rejectValue(propertyName, errorCode, rule.getErrorArguments(obj), rule.getDefaultErrorMessage());
            // simple, SimpleBeanTest.SimpleBean.simple[length], [se.stickybit.test.beans.SimpleBeanTest$SimpleBean@d1e233, 10, 255], ) invalid_length
            BindException errors = new BindException(sb, "target");
                    
            bv.validate(sb, errors);
            
            assertEquals("Validation should fail with one (1) errors since length < 10 (0)", 1, errors.getErrorCount());        
    
            System.out.println("ERROR COUNT: " + errors.getErrorCount());
            for (Object o : errors.getAllErrors())
            {
                ObjectError oe = (ObjectError)o;
                
                System.out.println(oe.toString());
                if (oe instanceof FieldError) {
                    FieldError fieldError = (FieldError) oe;
                    System.out.println(fieldError.getField());
                }
            }
       }
    
        @Validatable    
        public class SimpleBean
        {
            @Length(min = 10, max = 255, message ="invalid_length") //message = "feature name must be between 1-255 characters")
            private String simple;
    
            public void setSimple(String simple)
            {
                this.simple = simple;
            }
            
            public String getSimple()
            {
                return this.simple;
            }
        }
    }
    MyResourceBundle.properties:

    Code:
    invalid_length=property has an invalid length

  • #2
    i have the same problem

    have you fixed it?

    i explain my problem here:
    http://forum.springframework.org/sho...d.php?p=146194

    it's similar than yours.

    Comment

    Working...
    X