Spring MessageSource not being used during validation
Solution 1
@NotEmpty has a message property you can set on it. Since you're not setting it, it uses the default message.
@NotEmpty(message="{NotEmpty}")
should give you what you are looking for.
Solution 2
By default HibernateValidator will get all of its messages from /WEB-INF/classes/ValidationMessages.properties
It reads this file directly itself, not through the Spring MessageSource.
If you want translations from a Spring Message source, set one on the LocalValidatorFactoryBean. This does require you to manually set up a validation bean inside your dispatcher servlet, rather than rely on mvc:annotation-driven.
Solution 3
When you want to set the custom message for any field using JSR 303 validations then you need to specify the bean name(LoginForm) and field name(password) in key of the message in property file. The format is CONSTRAINT_NAME.COMMAND_NAME.FIELD_NAME
NotEmpty.loginform.password=This field should not be empty.
Jeremy
Updated on June 26, 2022Comments
-
Jeremy almost 2 years
I can't get my messages in messages.properties to be used during Spring validation of my form backing objects.
app-config.xml:
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> <property name="basename" value="messages" /> </bean>
WEB-INF/classes/messages.properties:
NotEmpty=This field should not be empty.
Form Backing Object:
... @NotEmpty @Size(min=6, max=25) private String password; ...
When I loop through all errors in the BindingResult and output the ObjectError's toString I get this:
Field error in object 'settingsForm' on field 'password': rejected value []; codes [NotEmpty.settingsForm.password,NotEmpty.password,NotEmpty.java.lang.String,NotEmpty]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [settingsForm.password,password]; arguments []; default message [password]]; default message [may not be empty]
As you can see the default message is "may not be empty" instead of my message "This field should not be empty".
I do get my correct message if I inject the messageSource into a controller and output this: messageSource.getMessage("NotEmpty", new Object [] {"password"}, "default empty message", null);
So why isn't the validation using my messages.properties? I'm running Spring 3.1.1. Thanks!
-
Jeremy almost 12 yearsYes if I set the message for each @NotEmpty it uses that. But I want it to use my messages.properties to get the default message for NotEmpty so I don't have to set it for every NotEmpty.
-
Joe almost 12 yearsI've never seen what you're talking about in Spring. Validations are field bound and should be handled as such. If you use a default message for all fields with the same problem it makes the feedback less valuable. An empty password is not the same as an empty username and giving a generic response such as "You have an empty field" really has no value.
-
Jeremy almost 12 yearsI know have spring using my messages.properties file by using LocalValidatorFactoryBean and adding a validator to my mvc:annotation-driven tag. You are right, I don't want to have a generic NotEmpty message for all fields. And adding that message attribute to NotEmpty does work, but I was hoping it would pick up my NotEmpty.password message in my messages.properties without explicitly telling it to use NotEmpty.password.
-
Joe almost 12 yearsThere's no convention like that, that I know of at this time. The system itself was designed to be extremely flexible. If you want that functionality you could add it in yourself.
-
Marko Vranjkovic over 8 yearsYou can also register a custom validation bean to be a default one with '<mvc:annotation-driven validator="yourValidationFactory" />'