phasews
Aug 23rd, 2004, 01:19 AM
I'm copying this over from the list and wanted to continue the discussion.
You should be able to make the argument itself MessageSourceResolvable. So…
MessageSourceResolvable nameResolvable = new MessageSourceResolvable() {
public String[] getCodes() {
return new String[] { “name” };
}
public Object[] getArgs() {
return null;
}
public String getDefaultMessage() {
return “Name”;
}
};
Then:
Object[] objArr = new Object[]{nameResolvable};
errors.rejectValue("name", "error.requried", objArr, "Name is required.");
We use this approach in the declarative validation stuff in the sandbox when constructing default error messages to display from the rules associated with domain object properties.
Keith
--------------------------------------------------------------------------------
From: springframework-user-admin@lists.sourceforge.net [mailto:springframework-user-admin@lists.sourceforge.net] On Behalf Of Brandon Goodin
Sent: Monday, August 23, 2004 12:52 AM
To: springframework-user@lists.sourceforge.net
Subject: [Springframework-user] Message Question
Greetings all,
I set up my messages resource in my xyz-servlet.xml as such:
<bean id="messageSource" class="org.springframework.context.support.ResourceBundle MessageSource" >
<property name="basename"><value>xyz-messages</value></property>
</bean>
I have the following message in my xyz-messages.properties:
error.required={0} is required.
I also have the name of my field in the xyz-messages.properties file as:
xyz.name=Name
In my Validator class, when a validation fails on the field I want to place the field name into the Object array, that is passed into the rejectValue method, using the message (xyz.name) from the messages.properties. How would I accomplish this?
String nameField = <how do I get the message?>;
Object[] objArr = new Object[]{nameField};
errors.rejectValue("name", "error.requried", objArr, "Name is required.");
Thanks,
Brandon
You should be able to make the argument itself MessageSourceResolvable. So…
MessageSourceResolvable nameResolvable = new MessageSourceResolvable() {
public String[] getCodes() {
return new String[] { “name” };
}
public Object[] getArgs() {
return null;
}
public String getDefaultMessage() {
return “Name”;
}
};
Then:
Object[] objArr = new Object[]{nameResolvable};
errors.rejectValue("name", "error.requried", objArr, "Name is required.");
We use this approach in the declarative validation stuff in the sandbox when constructing default error messages to display from the rules associated with domain object properties.
Keith
--------------------------------------------------------------------------------
From: springframework-user-admin@lists.sourceforge.net [mailto:springframework-user-admin@lists.sourceforge.net] On Behalf Of Brandon Goodin
Sent: Monday, August 23, 2004 12:52 AM
To: springframework-user@lists.sourceforge.net
Subject: [Springframework-user] Message Question
Greetings all,
I set up my messages resource in my xyz-servlet.xml as such:
<bean id="messageSource" class="org.springframework.context.support.ResourceBundle MessageSource" >
<property name="basename"><value>xyz-messages</value></property>
</bean>
I have the following message in my xyz-messages.properties:
error.required={0} is required.
I also have the name of my field in the xyz-messages.properties file as:
xyz.name=Name
In my Validator class, when a validation fails on the field I want to place the field name into the Object array, that is passed into the rejectValue method, using the message (xyz.name) from the messages.properties. How would I accomplish this?
String nameField = <how do I get the message?>;
Object[] objArr = new Object[]{nameField};
errors.rejectValue("name", "error.requried", objArr, "Name is required.");
Thanks,
Brandon