Announcement Announcement Module
Collapse
No announcement yet.
Localized messages in Validators Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Localized messages in Validators

    Hi
    In messages.properties I do have error code
    Code:
    field.invalid.string-longer = Field {0} must have at least {1} characters
    It requires two parameters, so in MyValidator I did sth like
    Code:
    ValidationTools.rejectIfStringTooLong(errors, "name", "field.invalid.string-longer", new Object[] { "Name",
                    getNameMax() }, null, getNameMax());
    Unfortunately "Name" was not localized, so I changed it to
    Code:
    private MessageSource messageSource;
    
    ...
    String name = messageSource.getMessage("label_myentity_name", null, locale);
    ValidationTools.rejectIfStringTooLong(errors, "name", "field.invalid.string-longer", new Object[] { name,
                    getNameMax() }, null, getNameMax());
    It works properly, but:
    * needs locale - I simply pass it from controller; pretty nasty
    * resolves this message's code even if it's not necessary

    I can create my own implementation of Errors class, that in method
    Code:
    void rejectValue(String field, String errorCode, Object[] errorArgs, String defaultMessage);
    would treat errorArgs as potential error codes, but this may cause problems with next Spring releases ?

    Does anybody know better solution ?

    best

    Lukasz
Working...
X