user1802047
user1802047

Reputation: 23

Spring Dynamic Validation

I am new to Spring validations. Previously i have used Struts validations. For dynamic validations, we will configure in errormessages.properties file like "errors.required={0} is required." later we will replace {0} with name. Is ther anyway in spring also for doing this. Please help me.

Thanks in advance.

Upvotes: 1

Views: 1305

Answers (2)

Tsimon Dorakh
Tsimon Dorakh

Reputation: 356

If you are implementing Spring Validator interface ( http://static.springsource.org/spring/docs/current/spring-framework-reference/html/validation.html#validator) - you can directly specify message arguments using

 void reject(String errorCode, Object[] errorArgs, String defaultMessage);

Upvotes: 2

ravindra
ravindra

Reputation: 186

Bellow code snippet may help you.

errormessages.properties

 errors.required={0} is required

you need to define ResourceBundleMessageSource bean in spring-context.xml.

<bean id="messageSource" class="org.springframwork.context.support.ResourceBundleMessageSource"> 
    <property name="messages">
         <list>
           <value>errormessages</value>
         <list>
    </property>
</bean>

In bean messageSource Member variable to access messages.

@Autowired
private MessageSource messageSource;

Second argument is array of object to pass.

messageSource.getMessage("errors.required",new Object[]{"Name"},"Default Required Error Message",null); 

Upvotes: 3

Related Questions